Posting to servlet from socket
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pavan_Danthala
Posted On:   Thursday, March 29, 2001 01:35 PM

I have a socket on my client side and I need to post data to servlet.
I could write data using 'GET' method, but I couldn't do it using 'POST'.
Any clues??

Re: Posting to servlet from socket

Posted By:   Michael_Wax  
Posted On:   Saturday, April 7, 2001 04:12 PM

Using something like the following should work. What this does is to create a URLConnection, write out the POST parameters, and then read in the reply from the remote server. Note that the parameter values should be URL encoded in case they contain any special characters.


//get connection and setup for post
URL url = new URL("http://www.whatever.com");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput (true);
connection.setUseCaches (false);

//write out post parameters
DataOutputStream outputStream = new DataOutputStream (connection.getOutputStream ());
String params = "first=" + URLEncoder.encode("value1") + ...;
outputStream.writeBytes (params);
outputStream.flush ();
outputStream.close ();

//read from the server
DataInputStream inputStream = new DataInputStream(connection.getInputStream ());
StringBuffer buffer = new StringBuffer;
String line;
while ((str = input.readLine()) != null) {
buffer.append(line);
}
inputStream.close ();
About | Sitemap | Contact