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.