Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HttpURLConnection

Reply
Thread Tools

HttpURLConnection

 
 
Siegfried Ertl
Guest
Posts: n/a
 
      11-30-2003
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)).openConnectio n());
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
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      11-30-2003
Siegfried Ertl wrote:
> 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?

<snip>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HttpURLConnection timeout solution Niels Campbell Java 6 10-27-2006 11:58 AM
HttpURLConnection.getInputStream() is returning null igoR Buttler Java 1 01-07-2004 06:36 PM
HttpURLConnection to Windows vs Lunux problems David G Java 3 12-16-2003 03:48 PM
trouble figuring out HttpURLConnection Flip Java 8 11-13-2003 05:33 AM
URGENT: How to retrieve 100 Continue responses using HttpURLConnection AKA Java 2 10-06-2003 01:58 PM



Advertisments