Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

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!

need explanations about HttpURLConnection, please help me !

843840Oct 10 2001 — edited Feb 27 2004
Hi !

I'm developping an applet which must communicate with a servlet to request data ! To establish the communication I do the following :

URL aURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection)aURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");

and to send a POST request I do :

OutputStreamWriter outStream = new OutputStreamWriter(new BufferedOutputStream(conn.getOutputStream()));
outStream.write("foo=bar");
outStream.flush();
BufferedReader in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
// Now I read in the response of the POST request
String inputline = in.readLine();
while( inputline != null ) {
System.out.println( inputline );
inputline = in.readLine();
}

All run perfectly well, but when I try to send a second POST request using the same HttpUrlConnection object the following exception is raised :

java.net.ProtocolException: Cannot write output after reading input.
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:358)
at Client.Client.main(Compiled Code)

So my question is : Do I create a new HttpUrlConnection for each request ? Why I can not use the same for severals request ?
My others questions about the same subjet are :
- When the connect() method of UrlConnection must be used ?
- If I create a new HttpUrlConnection for each request does it mean that each time a new socket is opened ?
- Where can I find more information about this subject (more details than in the JavaDocs) ?

Thanks a lot for your help.

Sebastien.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 26 2004
Added on Oct 10 2001
5 comments
572 views