HttpURLConnection

From: Siegfried Ertl (siegfried.ertl_at_web.de)
Date: 11/30/03


Date: 30 Nov 2003 10:34:48 -0800

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



Relevant Pages

  • Problem mit einfachem Socketserver/client
    ... Codeschnipsel aus der Client und der Serveranwendung. ... URL url = new URL; ... Der Client gibt "outputstream flushed" aus, der Server jedoch nur die ...
    (de.comp.lang.java)
  • Re: Socket programming
    ... > simple Indexing File name Server program. ... > I don't understand why no messages come out when the client connects. ... an ObjectInputStream by wrapping it around another InputStream, ... until it receives a header from its corresponding OutputStream. ...
    (comp.lang.java)
  • Reporting the progress of a servlet
    ... I have a servlet that needs to report its progress and stop if the user ... As a test I have tried using the outputstream of the ... HttpServletResponse to flush dots to the client. ...
    (comp.lang.java.programmer)