Re: Post using Query String
From: Nikk & Jak Anderson (nikk_and_jak_at_btinternet.com)
Date: 03/17/04
- Previous message: anonymous_at_coolgroups.com: "rmi port"
- In reply to: Michael Scovetta: "Re: Post using Query String"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 17 Mar 2004 12:06:53 -0800
To POST from your java application is fairly straigtforward.
It is very similar to a get, except you must set the method (get/post)
the in request header. Then you need to write the post data out to a
stream.
Example code:
/*
* _url is the url of the server
* _request is the data you want to post
*/
public String getResponse (URL _url, String _request)throws Exception{
HttpURLConnection c = null;
InputStream is = null;
OutputStream os = null;
// b is to collect the repsonse from the server
StringBuffer b = new StringBuffer( );
// open the connection
c = (HttpURLConnection)_url.openConnection();
// set the http headers
c.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
c.setRequestProperty("Content-Length", "" + Integer.toString
(_request.getBytes().length) );
c.setDoOutput(true);
c.setDoInput(true);
// set the method (i.e. GET or POST)
c.setRequestMethod("POST");
// get the output stream - this is for writing the post data to
os = c.getOutputStream();
// write the data
os.write(_request.getBytes());
os.flush();
os.close();
// Get HTTP response
is = c.getInputStream( );
System.out.println("Response code = "+ c.getResponseCode());
int ch;
while ((ch = is.read( )) != -1){
b.append((char) ch);
}
is.close();
c.disconnect();
// b contains the data returned from the server
return(b.toString());
}
- Previous message: anonymous_at_coolgroups.com: "rmi port"
- In reply to: Michael Scovetta: "Re: Post using Query String"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|