Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

closing connection with httpUrlConnection

843790Aug 10 2010 — edited Aug 10 2010
Dears ,

i have posted this before but i guess code was not pasted in correct format.. so i here i go again ...

All iam trying is to post multiple xml requests to a remote host and get responses for each request using httlurlconnection .

I used the following code in a loop to do so :Howvere iam not pasting the xml

for(loop)
String xml request = "xml request..n"
URL url = new URL(Config.getProperty("url"));
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type",
"application/soap; charset=\"utf-8\"");
OutputStream stream = httpConnection.getOutputStream();
stream.write(request.getBytes());
stream.close();
InputStream stream = httpConnection.getInputStream();
BufferedInputStream bufferedStream = new BufferedInputStream(stream);
InputStreamReader reader = new InputStreamReader(bufferedStream);
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuffer buffer = new StringBuffer();
while (true) {
String string = bufferedReader.readLine();
if (string == null) {
break; }
buffer.append(string);
}
bufferedReader.close();
reader.close()
bufferedStream.close();
stream.close();
httpConnection.disconnect();
}

Doing so .. for first request.. i successfuly get response but for every second response..it fails with a broken pipe .. Socket reset exception ...

If i remove the line httpConnection.disconnect in the end iam able to get responses for very request .

So my question is , is it the correct way to avoid the probelm ? Or are there any best approaches to solve this problem ?

Thanks in advacne
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 7 2010
Added on Aug 10 2010
5 comments
1,605 views