Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Program a POST request?

Thread view: 
Peter Kirk - 16 Mar 2004 11:36 GMT
Hi there,

can someone help, or point me to a resource on the web where I can find out
how to programmatically generate an HTTP post request (including parameters)
to a servlet.

I am trying to programmatically upload files to a servlet, along with what
would be "hidden" fields on an HTML form.

For example, my HTML form might be like the following. But how do I simulate
this programmatically by creating my own HttpServletRequest?

<form name="uploadform" method="post" action="/upload"
enctype="multipart/form-data">
 <input type="hidden" name="id_param" value="678">
 <input type="hidden" name="name_param" value="picture">
 <input type="file" name="uploadfile" size="75">
 <input class="knap" type="submit" value="upload">
</form>

Thanks,
Peter
Markos Charatzas - 16 Mar 2004 11:50 GMT
Have a look at HttpClient by Apache

http://jakarta.apache.org/commons/httpclient/

Regards

> Hi there,
>
[quoted text clipped - 22 lines]
> Thanks,
> Peter
Ryan Stewart - 16 Mar 2004 13:46 GMT
> Hi there,
>
[quoted text clipped - 18 lines]
> Thanks,
> Peter

http://www.ietf.org/rfc/rfc2616.txt
RC - 16 Mar 2004 14:26 GMT
> Hi there,
>
[quoted text clipped - 15 lines]
>   <input class="knap" type="submit" value="upload">
> </form>

The O'Reilly books always the best resources.
Servlet Programming, JSP and Network Programming.
All these books have mention what you need to know.
Dave Miller - 16 Mar 2004 17:46 GMT
> Hi there,
>
[quoted text clipped - 18 lines]
> Thanks,
> Peter

Here is a code snippet:

URL url = new URL("http://www.your_servlet.com);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.addRequestProperty("name it", "value it"); //repeat as
necessary
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream
());
FileInputStream fin = new FileInputStream(your_file);
while ((i = fin.read()) != -1) {
out.write(i);
}
out.flush();
out.close();

Signature

Dave Miller
FundablePlans - Create a custom business plan online - only $39.95
http://www.fundableplans.com

Princess Morgiah - 16 Mar 2004 21:33 GMT
> Hi there,
>
[quoted text clipped - 15 lines]
>   <input class="knap" type="submit" value="upload">
> </form>

Don't. Open up a socket pointing to the web server of choice and send out a
HTTP 1.0/1.1 POST.

Or you could try the solution Dave Miller pointed out, which is a lot less
'dirty'.

Princess Morgiah
Roedy Green - 17 Mar 2004 01:42 GMT
>can someone help, or point me to a resource on the web where I can find out
>how to programmatically generate an HTTP post request (including parameters)
>to a servlet.

see http://mindprod.com/fileio.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.