• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple posts using HttpURLConnection

 
damian cosmos
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to connect to a URL to write multiple POST strings to the URL in the form name=value&name=value etc.
This works, the first time, as I get a html string corressponding to the next page in the sequence, when I use a BufferedReader to read the response.
A problem arises when I try to connect to the URL with the next POST string.
Nothing comes back.
The following is a code snippet of what I'm trying to do:
// open the connection and prepare it to POST
URL url = new URL("http://www.whatever.com");
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setAllowUserInteraction(false);
DataOutputStream dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
String query = "name=value&name=value....."
// Send the data
dataOutputStream.writeBytes(query);
// Read the response
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String string = new String("");
StringBuffer stringBuffer = new StringBuffer();
while((string = bufferedReader.readLine()) != null)
{
stringBuffer.append(string);
}
// Second page
String query1 = "name=value&name=value....."
// Send the data
dataOutputStream.writeBytes(query1);
// Read the response
String string1 = new String("");
StringBuffer stringBuffer1 = new StringBuffer();
while((string1 = bufferedReader.readLine()) != null)
{
stringBuffer1.append(string1);
}
Also is it possible to ascess session data using the above senario as the HttpURLConnection is persistant ?
Any help would be greatly appreciated.
Regards,
Damian.
 
Lewin Chan
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Everytime you want to send a new request, you should do

With Http You have to send the correct headers each time you make a request. I'm unsure of the reference sun http protocol handler, but I would think that the HTTP headers are written if and when you do a openConnection (or even when you do a getOutputStream)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!