7

我正在使用 WPF .net 4.0 应用程序。我有一个搜索栏。对于每个搜索令牌,我需要对 8 个单独的 URL 执行 8 个 http 请求以获取搜索结果。一旦用户停止在搜索栏中输入,我会在 400 毫秒后向服务器发送 8 个请求。搜索 6 到 7 个搜索标记结果非常好。但在那之后,HttpWebRequest 突然停止工作。没有抛出异常,没有收到响应。我正在使用 Windows 7,我也禁用了防火墙。我不知道后续的http请求丢失在哪里。

谁能告诉我灯来解决这个问题?

下面是我的 HttpWebRequest 调用代码。

public static void SendReq(string url)
{
    // Create a new HttpWebRequest object.
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    request.ContentType = "application/x-www-form-urlencoded";
    request.Proxy = new WebProxy("192.168.1.1", 8000);

    // Set the Method property to 'POST' to post data to the URI.
    request.Method = "POST";

    // start the asynchronous operation
    request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

}

private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    Stream postStream = request.EndGetRequestStream(asynchronousResult);

    string postData = this.PostData;

    // Convert the string into a byte array. 
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    // Write to the request stream.
    postStream.Write(byteArray, 0, byteArray.Length);
    postStream.Close();

    // Start the asynchronous operation to get the response
    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}

private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    using(HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
    {
        using(Stream streamResponse = response.GetResponseStream())
        {
             using(StreamReader streamRead = new StreamReader(streamResponse))
             {
                 string responseString = streamRead.ReadToEnd();
                 Debug.WriteLine(responseString);
             }
        }
    }
}
4

4 回答 4

6

我想我很晚了,但我仍然想回答你的问题,也许它可以对其他人有所帮助。默认情况下,您发出的 HTTP 请求是 HTTP 1.1 请求。HTTP 1.1 请求默认有Keep-Alive连接。因此,当您向同一服务器 .net 框架发出太多请求时,只会使 x 不。的请求。

您应该通过以下方式关闭所有回复response.Close()

您还可以指定可以同时发出多少个请求。

ServicePointManager.DefaultConnectionLimit = 20;

请注意,您必须DefaultConnectionLimit在您提出的第一个请求之前进行设置。您可以在 msdn 上找到更多信息 。

于 2014-03-23T10:09:38.957 回答
2

我所看到的只是GetRequestStreamCallback你应该替换

postStream.Write(byteArray, 0, postData.Length);

经过

postStream.Write(byteArray, 0, byteArray.Length);

因为这些长度不一定相等。

于 2012-12-21T13:09:13.620 回答
0

@Somnath I am not sure if you found this answer, but if anyone else stumbles across this post with the same issue that Somnath and I were having.

We all try to do our due diligence in keeping memory clean and clear, but with streams we always will save unexplained issues if we make sure to flush the stream before we close it.

Replace This :

postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();

With This :

postStream.Write(byteArray, 0, byteArray.Length);
postStream.Flush();
postStream.Close();
于 2013-02-02T20:07:04.883 回答
-1

我遵循了你们所有人提供的所有建议,但无法阻止 HTTP 请求的静默失败。但我找到了解决方法。直到现在,连我自己也无法得出最终的结论。

但是我的解决方法现在运行良好,没有任何失败。

SendReq(string url)函数中,我添加了以下代码行

System.Net.ServicePointManager.DefaultConnectionLimit = 100; // Just selected a random number for testing greater than 2
System.Net.ServicePointManager.SetTcpKeepAlive(true, 30, 30); // 30 is based on my server i'm hitting
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

request.KeepAlive = true;
于 2013-02-14T05:49:22.990 回答