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();
        }
    }

https 経由でページを読み込もうとすると、算術演算でオーバーフローが発生します。応答で ContentLenght = -1 が返されるため、エラーが発生します。fiddler を使用すると、ページが受信されたことがわかります。HTTPS を使用する他の Web サイトの一部は正常に動作しますが、ほとんどの Web サイトは正常に動作しません。

4

3 に答える 3

1

https://www.google.comにクエリを実行すると、すべての応答にコンテンツの長さが含まれているわけではないため、同じエラーメッセージが表示されます。このコードを使用して、問題を回避します。

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

この動作は予想されるものです。すべての応答にコンテンツの長さが含まれているわけではありません。

サンプルには長さを知る必要のあるものは何もないので、単にそれを読まないだけで十分かもしれません。

于 2013-01-10T18:16:40.527 に答える
0

HttpWebResponse.ContentLength プロパティから

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.

ヘッダーが設定されContent-Lengthていなくても、応答が悪いわけではありません。

于 2013-01-10T18:24:14.833 に答える