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