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) {
}
Ich glaube der Key darf nicht UrlEncoded werden ?!?
Bitte um Rückmeldung.
Hey it saved a lot of time. Thanks
hey geeks-online.de ... what about keys that go: ab?c or a&b or even a=b ?? they would break everything
I think this is the get method, not post
It will throw exception when the program excutes the statment " OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());"
How to fix it??
:)
good example/// :)
'">lol<
worked great!
thanks for the tip!
Simple and good, thnx 4 the bunch.
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?
Thank for your example, it's very useful to me.
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?
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.
Nice example!
awesome dude! solved my 2 days of search effort :)
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?
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.