Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpWebRequest的响应为空,请求从Bing进行搜索_C#_Httpwebrequest_Bing - Fatal编程技术网

C# HttpWebRequest的响应为空,请求从Bing进行搜索

C# HttpWebRequest的响应为空,请求从Bing进行搜索,c#,httpwebrequest,bing,C#,Httpwebrequest,Bing,我有以下代码向Bing发送HttpWebRequest。当我请求下面的url时,虽然它返回的是一个空响应,但它应该返回一个结果列表 var response = string.Empty; var httpWebRequest = WebRequest.Create("http://www.bing.com/search?q=stackoverflow&count=100") as HttpWebRequest; httpWebRequest.Method = WebRequestMe

我有以下代码向Bing发送HttpWebRequest。当我请求下面的url时,虽然它返回的是一个空响应,但它应该返回一个结果列表

var response = string.Empty;
var httpWebRequest = WebRequest.Create("http://www.bing.com/search?q=stackoverflow&count=100") as HttpWebRequest;

httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Headers.Add("Accept-Language", "en-US");
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)";
httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

using (var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse)
{
    Stream stream = null;
    using (stream = httpWebResponse.GetResponseStream())
    {
        if (httpWebResponse.ContentEncoding.ToLower().Contains("gzip"))
            stream = new GZipStream(stream, CompressionMode.Decompress);
        else if (httpWebResponse.ContentEncoding.ToLower().Contains("deflate"))
            stream = new DeflateStream(stream, CompressionMode.Decompress);

        var streamReader = new StreamReader(stream, Encoding.UTF8);
        response = streamReader.ReadToEnd();
    }
}
它是请求和接收网页的标准代码。你知道为什么回复是空的吗?提前谢谢

编辑我在url中遗漏了一个查询字符串参数。我还有&count=100,我现在已经更正了。它似乎适用于50及以下的值,但较大时不会返回任何值。这在浏览器中正常工作,但不适用于此web请求


这让我觉得问题在于响应很大,而HttpWebResponse并没有按照我设置的方式来处理。不过只是一个猜测。

这在我的机器上运行得很好。也许你被禁止使用Bing?

你的代码在我的机器上运行良好


我建议您自己弄一份,并检查实际发生的HTTP会话。可能是代理或防火墙

谢谢Jan。我可以使用浏览器搜索bing,这样我就不会被禁止。当&count=100时,我的浏览器和应用程序中都不会显示任何结果。这就是问题所在。尝试此库:感谢您的回复。我省略了url的count查询字符串参数。我把它设置为计数=100。