HttpURLConnection

S

Siegfried Ertl

Hi Group,

i want to make a write and read communication with a HttpURL
Connection.
I use the method POST. But when i want to get the Outputstream of the
connection
it throws the error message "Cannot write output after reading input".
Can anyone tell me what i did wrong with the following code?

This is the client side:

HttpURLConnection huc = (HttpURLConnection)((
new URL(MobilityConstants.URL_MOBILITY)).openConnection());
huc.setRequestMethod("POST");
huc.setDoInput(true);
huc.setDoOutput(true);
huc.setUseCaches(false);
huc.setRequestProperty("Accept", "text/xml");
huc.setRequestProperty("Connection", "keep-alive");
huc.setRequestProperty("Content-Type", "text/xml");
huc.setRequestProperty( "Content-length", Integer.toString

(bos.toString().length()));
huc.connect();

if (huc.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println(huc.getResponseMessage());
}
else{
OutputStream out = huc.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(huc.getOutputStream()));
<--HERE IT
THROWS EXCEPTION "Cannot write output after reading
input"

writer.write(bos.toString());
writer.flush();
writer.close();


BufferedReader reader = new BufferedReader(
new InputStreamReader(huc.getInputStream()));
String line = reader.readLine();
StringBuffer content = new StringBuffer();
while(line!=null)
{
content.append(line+"\n");
line = reader.readLine();
}
System.out.println(content.toString());
reader.close();
huc.disconnect();
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

Here is the server side.

public class DirectRequest extends HttpServlet{

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{

try{
ObjectInputStream ois = new ObjectInputStream(
request.getInputStream()); <- FROM THAT POINT, THE SERVLET
THROWS THE EXCEPTION
Object obj = ois.readObject();
ois.close();

String xmlSrc = (String)obj;
XMLCreator creator = new XMLCreator();
creator.setDoc(xmlSrc);


PrintWriter out = response.getWriter();
out.write(creator.getDocAsString());
}
catch(Exception e){
PrintWriter out = response.getWriter();
out.print(e.getMessage());
}
}
}

For any help, i would be very thanksful. The best would be a small
example.
Please dont tell me the jakarta httpClient package. I cant use it,
because the client is a pda and the api is 1.1.8 standard.

Thanks for help,

Sigi
 
S

Sudsy

Siegfried said:
Hi Group,

i want to make a write and read communication with a HttpURL
Connection.
I use the method POST. But when i want to get the Outputstream of the
connection
it throws the error message "Cannot write output after reading input".
Can anyone tell me what i did wrong with the following code?
if (huc.getResponseCode() != HttpURLConnection.HTTP_OK) {
This causes a read----+
System.out.println(huc.getResponseMessage());
}
else{
OutputStream out = huc.getOutputStream();

Now you want to write.

The invocations are out of order. Do all of your writing before trying
to open the input stream, including trying to get the response code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top