HttpURLConnection

Discussion in 'Java' started by Siegfried Ertl, Nov 30, 2003.

  1. 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
     
    Siegfried Ertl, Nov 30, 2003
    #1
    1. Advertising

  2. Siegfried Ertl

    Sudsy Guest

    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.
     
    Sudsy, Nov 30, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    8
    Views:
    677
  2. David G
    Replies:
    3
    Views:
    2,967
    David G
    Dec 16, 2003
  3. igoR Buttler
    Replies:
    1
    Views:
    12,158
  4. Niels Campbell

    HttpURLConnection timeout solution

    Niels Campbell, Jan 23, 2004, in forum: Java
    Replies:
    6
    Views:
    43,781
    dadada
    Oct 27, 2006
  5. tony vee

    HttpUrlConnection Problems

    tony vee, Feb 6, 2004, in forum: Java
    Replies:
    2
    Views:
    5,935
    tony vee
    Feb 6, 2004
Loading...

Share This Page