Sending a POST Request Using a URL

See also Sending a POST Request Using a Socket.
try { // Construct data String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8"); data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8"); // Send data URL url = new URL("http://hostname:80/cgi"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { // Process line... } wr.close(); rd.close(); } catch (Exception e) { }

Comments

14 Feb 2010 - 7:47pm by geeks-online.de (not verified)

Ich glaube der Key darf nicht UrlEncoded werden ?!?

Bitte um Rückmeldung.

16 Feb 2010 - 5:49am by Anonymous (not verified)

Hey it saved a lot of time. Thanks

16 Feb 2010 - 8:22am by Xandl (not verified)

hey geeks-online.de ... what about keys that go: ab?c or a&b or even a=b ?? they would break everything

23 Feb 2010 - 8:22am by Anonymous (not verified)

I think this is the get method, not post

3 Mar 2010 - 6:12am by Anonymous (not verified)

It will throw exception when the program excutes the statment " OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());"
How to fix it??

9 Mar 2010 - 11:50pm by Anonymous (not verified)

:)

9 Mar 2010 - 11:51pm by Anonymous (not verified)

good example/// :)

18 Mar 2010 - 6:17am by Anonymous'">< (not verified)

'">lol<

19 Mar 2010 - 1:09am by zzzlyle (not verified)

worked great!
thanks for the tip!

7 Apr 2010 - 3:35am by Muhammad Salman Ansari (not verified)

Simple and good, thnx 4 the bunch.

28 May 2010 - 9:23am by Anonymous (not verified)

When I run this its just displaying the webpage specified in the url, how can we get it to display the data from the submitted form with the specified urlEncoder parameters?

1 Jun 2010 - 1:09am by loyality (not verified)

Thank for your example, it's very useful to me.

23 Jun 2010 - 11:13am by Anonymous (not verified)

HTML has two methods for calling URLs -- POST and GET. This method is using "GET". How do you perform "POST" using this method? Is it possible?

23 Jun 2010 - 11:47am by Anonymous (not verified)

Actually the code above is POST. Basically if you take the "data" and concatinate to the URL then it becomes GET. OutputStreamWriter makes it POST.

1 Jul 2010 - 3:26pm by Anonymous (not verified)

Nice example!

10 Jul 2010 - 6:28pm by Anonymous (not verified)

awesome dude! solved my 2 days of search effort :)

14 Jul 2010 - 7:34am by Anonymous (not verified)

when I send this to an ASPX page, my ASPX page doesn't seem to get the variable that I'm passing.
This is the aspx:
fnameID=Request.QueryString("fnameID")
Response.Write("variable passed was " & fnameID)
When I passed my fnameID with a value of 1 (using the same method you used with "key1", all I get returned to me is "variable passed was ". The value of the variable passed by the Java application doesn't seem to be retrieved by the ASPX page. Any ideas?

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.