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?