0
public void HttpsRequest(string address)
    {
        string data;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
        request.Method = "GET";

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        byte[] resp = new byte[(int)response.ContentLength];
        Stream receiveStream = response.GetResponseStream();
        using (StreamReader reader = new StreamReader(receiveStream, Encoding.ASCII))
        {
            data = reader.ReadToEnd();
        }
    }

I get an Arithmetic operation resulted in an overflow when I am trying to read a page over https. Errors occur because the response gives me ContentLenght = -1. Using fiddler I can see that the page was received. Some other websites using HTTPS works fine but most of them not.

4

3 回答 3

1

If I query https://www.google.com, I get the same error message, because not every response has a content length. Use this code to avoid the problem:

public static void HttpsRequest(string address)
{
  string data;
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
   request.Method = "GET";

   using (WebResponse response = request.GetResponse())
  {
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        data = reader.ReadToEnd();
    }
  }
}
于 2013-01-10T18:17:08.917 回答
0

This behavior is expected: not every response contains content length.

There is nothing in your sample that requires length to be known, so simply not reading it maybe enough.

于 2013-01-10T18:16:40.527 回答
0

From HttpWebResponse.ContentLength Property

The ContentLength property contains the value of the Content-Length header returned with the response. If the Content-Length header is not set in the response, ContentLength is set to the value -1.

If Content-Length header is not set it does not mean that you got a bad response.

于 2013-01-10T18:24:14.833 回答