×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    14

    7650: HttpConnection.GET limit on data size?

    THE PROBLEM
    -----------
    I'm using an HttpConnection to GET some data off a server
    (the actual code FYI is provided at the end of this message.)
    It works OK, when the downloaded file size is up to 4KB, and it
    fails when more than 4KB (this was determined by inspection:
    I tried a file of 3.9KB and it worked OK, and then I tried a file
    of 4.7KB and it failed.)

    THE ERROR
    ---------
    When the file exceeds approx. 4KB the error that I'm getting is:
    CWin32AsyncStreamSocket::HandleEvent: WSAEnumNetworkEvents(...) failed
    Please note that even during the above error occures the HttpConnection
    has returned a HTTP_OK code!

    THE CODE
    --------
    FYI, the code is provided here. Please note that IT WORKS FINE
    when the size of the downloaded file (at the specified URL) is small
    (approx. less than 4KB). It reportedly has succeeded for a filesize
    of 3.9KB. It reportedly has failed for a filesize of 4.7KB.

    Here's the code:

    public class HttpDownload {
    private static HttpConnection http = null;
    private static InputStream is = null;

    public HttpDownload(String url) throws IOException {

    try {
    http = (HttpConnection) Connector.open(url);

    http.setRequestMethod(HttpConnection.GET);
    http.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"

    String strMsg = http.getResponseMessage();
    int iCode = http.getResponseCode();

    if (iCode == HttpConnection.HTTP_OK) {
    is = http.openInputStream();
    int length = (int) http.getLength();

    if (length != -1) {
    // Read data in one chunk
    byte serverData[] = new byte[length];
    is.read(serverData);
    strServerData = new String(serverData);
    }
    else // Length not available
    {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Read data one character at at time
    int ch;
    while ((ch = is.read()) != -1) {
    baos.write(ch);
    }
    strServerData = new String(baos.toByteArray());
    baos.close();
    }
    }
    }
    catch (IOException ioe)
    {
    throw new IOException("My message here..."
    }
    }


    MY QUESTION
    -----------
    Anyone who have some insight on the above problem please post a message.
    Thanks in advance.

    Alex.

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    101

    RE: 7650: HttpConnection.GET limit on data size?

    InputStream.read(byte[] b) may (according to the javadoc) read fewer than b.length bytes. You need to check the return value from

    is.read(serverData);

    and continue reading if the return value is less than the expected length (unless the return value is -1, in which case there is no more data).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×