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?

3 Sep 2010 - 2:41am by mbt kisumu (not verified)

There are many new classic tall ugg boots fashion style shoes, discount ugg boots and discount uggs is the new design to help you get healthy and fitness, there are many fans can share the experience.

It is necessary for you to own a pair of mbt m.walk, for mbt karani help you keep healthy and fitness, and as to OL workers, owning a pair of mbt kisumu shoes, it is the good choice.

Don't hessitate to tell me your experience about air jordan 12 shoes, for I have owned a pair of mbt lami long time, and I like it very much, this time I will choose mbt kaya one.
What is the good choice for you to own a pair of shoes in this hot summer? How about UGG Lo Pro or some other ugg ultra short style, believing in they are at least one ugg classic short black style can fit you.

Have you ever heard kids ugg boots? ugg boots discount is the new style design to fit mbt's fans, including classic cardy ugg boots , it is also the cool choice.

More discount for you to enjoy the baby ugg boots, after get discount you can also enjoy the gifts , so don't lose chance nightfall ugg boots and bailey button triplet ugg boots are good choice.

mbt lami are suitable to this hot summer, it is cool design can help people to protect feet. So mbt kaya and monster headphones are all good supply. Come and join us.

Everybody need to wear shoes everyday and like to get a good pair of shoes to do sport, here I recommed monster beats by dr dre, which is a new style of A href=" http://www.monsterbeatsbydr.com ">dr dre headphones, besides, lady gaga headphones is also a good choice, just enjoy them.

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.