Tengyu Liu
Tengyu Liu

Reputation: 1253

c# WebResponse content length limit

I'm trying to fetch a big file from internet with C# with WebRequest. So far all other stuff that I have is working fine, but it appears that some part in the middle of the response I get is removed. I tried printing out the response.ContentLength and getResponseContent(response).Length and I got -1 and 80000 respectively. Is there a limit on the WebResponse length or it's the helper function that's buggy? How do I get the full content?

Here's my getResponseContent function:

    private static String getResponseContent(HttpWebResponse response)
    {
        Stream responseStream = response.GetResponseStream();

        byte[] buffer = new byte[1000];
        String ret = "";

        while (responseStream.Read(buffer, 0, 1000) > 0)
            ret += (System.Text.Encoding.Default.GetString(buffer));

        return ret;
    }

Thanks to @Neolisk, I've re-written my getResponseContent to use the StreamReader class and it works like magic. Here's the code:

    private static String getResponseContent(HttpWebResponse response)
    {
        Stream responseStream = response.GetResponseStream();
        StreamReader sr = new StreamReader(responseStream);
        return sr.ReadToEnd();
    }

However, still, can anyone explain why the the ContentLength in the response header is -1 instead some meaningful length?

Upvotes: 2

Views: 4150

Answers (3)

Eric Lemes
Eric Lemes

Reputation: 581

The ContentLength is a http header. If the server side didn't send it you'll receive -1. It's not mandatory.

That's why using the ResponseStream is a good idea.

Upvotes: 1

usr
usr

Reputation: 171178

You are not using the return value from responseStream.Read to find out how many bytes were received.

Upvotes: 1

slippyr4
slippyr4

Reputation: 862

Without knowing what the actual data you're receiving is, it's hard to be sure what's going on.

However - response.ContentLength is the header value from the HTTP response, which is the length of the response in bytes.

How many characters in a string that represents is not necessarily the same thing - many unicode type encodings are more than one byte per character.

Use the responseStream.Read overload that puts binary data into a byte[] buffer, create a decoder based on what the actual encoding is (set in the header) and decode your string from there.

Upvotes: 1

Related Questions