HttpUrlConnection => reading all packets

Discussion in 'Java' started by Terrie, Nov 19, 2004.

  1. Terrie

    Terrie Guest

    Dear all,

    in my project I have to contact a server and read a big amount of byte
    data. I am using HTTPUrlConnection and have to use POST. All works
    fine but when the amount of data is bigger then one packet size (appr.
    1450 Byte) then the rest of the data is omitted. Does anybody know how
    to read ALL packets that are send vie HTTP Post??

    Any ideas?

    Thx,

    Sabine
    Terrie, Nov 19, 2004
    #1
    1. Advertising

  2. Terrie

    Murray Guest

    "Terrie" <> wrote in message
    news:...
    > Dear all,
    >
    > in my project I have to contact a server and read a big amount of byte
    > data. I am using HTTPUrlConnection and have to use POST. All works
    > fine but when the amount of data is bigger then one packet size (appr.
    > 1450 Byte) then the rest of the data is omitted. Does anybody know how
    > to read ALL packets that are send vie HTTP Post??
    >
    > Any ideas?
    >
    > Thx,
    >
    > Sabine


    Why don't you post what you've done, then we can suggest what might be wrong
    ....
    Murray, Nov 19, 2004
    #2
    1. Advertising

  3. Terrie

    shakah Guest

    You have to keep reading the response until you get the amount of bytes
    specified in the response's Content-length header. Here's a code
    excerpt that does a POST and reads a response:

    java.net.URL url = new java.net.URL(sURL) ;
    java.net.URLConnection urlc = url.openConnection() ;
    urlc.setDoInput(true) ;
    urlc.setDoOutput(true) ;
    urlc.setUseCaches(false) ;
    urlc.setRequestProperty("Content-Type", "text/xml") ;
    urlc.setRequestProperty("Content-Length", ""+sPostData.length()) ;
    urlc.setRequestProperty("Connection", "close") ;

    // ...write the POST data
    java.io_OutputStream os = urlc.getOutputStream() ;
    os.write(sPostData.getBytes()) ;
    os.flush() ;

    // ...read the response
    java.io.InputStream is = urlc.getInputStream() ;
    int nContentLength = urlc.getContentLength() ;
    byte [] ab = new byte[nContentLength] ;
    int nRead=0 ;
    while(nRead < nContentLength) {
    nRead += is.read(ab, nRead, nContentLength - nRead) ;
    }
    sbResponse.append(new String(ab, "utf-8")) ;
    os.close() ;
    os = null ;
    is.close() ;
    is = null ;
    ((java.net.HttpURLConnection) urlc).disconnect() ;
    urlc = null ;
    shakah, Nov 19, 2004
    #3
  4. Terrie

    Terrie Guest

    Hi shakah,

    thanks for the response but my program still doesn't get the whole
    content. I'll post my code:

    HttpURLConnection httpcon =
    (HttpURLConnection)url.openConnection();
    String requestText = request.getRequest();
    httpcon.setDoOutput(true);
    httpcon.setDoInput(true);
    httpcon.setUseCaches(false) ;
    httpcon.setRequestProperty("Content-Type", "text/xml") ;
    httpcon.setRequestProperty("Content-Length",
    ""+requestText.length()) ;
    httpcon.setRequestProperty("Connection", "close") ;
    httpcon.setRequestMethod("POST"); // HEAD
    OutputStream os = httpcon.getOutputStream();
    PrintWriter bw = new PrintWriter(httpcon.getOutputStream(),true);
    bw.write(requestText);
    bw.flush();

    if(httpcon.getResponseCode() != HttpURLConnection.HTTP_OK) {
    System.out.println(httpcon.getResponseMessage());
    }
    else {
    InputStream is = httpcon.getInputStream();
    int len = httpcon.getContentLength();
    new EvalData(is, len);
    // here I print out the is
    }

    Thx
    Terrie, Nov 22, 2004
    #4
  5. Terrie

    Terrie Guest

    forget the last post... it works fine now. Thanks!!!
    Terrie, Nov 22, 2004
    #5
  6. Terrie

    JScoobyCed Guest

    Terrie wrote:
    > Hi shakah,
    >
    > thanks for the response but my program still doesn't get the whole
    > content. I'll post my code:
    >
    > HttpURLConnection httpcon =
    > (HttpURLConnection)url.openConnection();
    > String requestText = request.getRequest();
    > httpcon.setDoOutput(true);
    > httpcon.setDoInput(true);
    > httpcon.setUseCaches(false) ;
    > httpcon.setRequestProperty("Content-Type", "text/xml") ;
    > httpcon.setRequestProperty("Content-Length",
    > ""+requestText.length()) ;
    > httpcon.setRequestProperty("Connection", "close") ;
    > httpcon.setRequestMethod("POST"); // HEAD
    > OutputStream os = httpcon.getOutputStream();
    > PrintWriter bw = new PrintWriter(httpcon.getOutputStream(),true);
    > bw.write(requestText);
    > bw.flush();
    >
    > if(httpcon.getResponseCode() != HttpURLConnection.HTTP_OK) {
    > System.out.println(httpcon.getResponseMessage());
    > }
    > else {
    > InputStream is = httpcon.getInputStream();
    > int len = httpcon.getContentLength();
    > new EvalData(is, len);
    > // here I print out the is
    > }
    >
    > Thx



    Did u try to count how many CR+LF is there in your response ? I remember
    such problem while reading an http response: the Content-Lenght was ,
    say, 1000. I read 1000 ans I was missing a part of the content, let's
    say 100. Then I read the InputStream without limit (and stopped the
    program by forced shutdown), and counted the number of CR+LF: it was 100 !
    Then I changed my code to detect CR+LF and only increment by one for the
    set of two bytes, instead of one per byte. And I got it working.
    Now that was several years ago, and the servers where not the same
    (Apache 1.3.1x ... not sure, under Solaris, and a windows 98 plateform
    to run the Java program).

    JScoobyCed
    JScoobyCed, Nov 22, 2004
    #6
    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. Abraham Khalil
    Replies:
    0
    Views:
    406
    Abraham Khalil
    Jan 26, 2004
  2. antoine
    Replies:
    3
    Views:
    4,805
    antoine
    Dec 7, 2005
  3. Andre
    Replies:
    1
    Views:
    361
  4. Tomas Christiansen

    Send all packets (socket programming)

    Tomas Christiansen, May 22, 2005, in forum: Python
    Replies:
    0
    Views:
    396
    Tomas Christiansen
    May 22, 2005
  5. Pashlipops

    Reading packets from UDP port

    Pashlipops, Oct 15, 2006, in forum: C++
    Replies:
    3
    Views:
    858
Loading...

Share This Page