简体   繁体   English

HttpWebRequest突然停止工作,几个请求后没有收到响应

[英]HttpWebRequest stops working suddenly, No response received after few requests

I'm working with a WPF .net 4.0 Application. 我正在使用WPF .net 4.0应用程序。 I have a search bar. 我有一个搜索栏。 For each search token I need to do 8 http request to 8 separate URLs to get search results. 对于每个搜索令牌,我需要对8个单独的URL执行8个http请求以获取搜索结果。 I send 8 requests to server after 400 milliseconds once the user stops typing in search bar. 一旦用户停止在搜索栏中输入,我会在400毫秒后向服务器发送8个请求。 Searching for 6 to 7 search-tokens results comes very nicely. 搜索6到7个搜索令牌的结果非常好。 But after that suddenly HttpWebRequest stops working silently. 但在那之后突然HttpWebRequest停止了默默工作。 No exception was thrown, no response was received. 没有例外,没有收到任何回复。 I'm working with Windows 7, I disabled the firewall too. 我正在使用Windows 7,我也禁用了防火墙。 I don't know where the subsequent http requests are lost. 我不知道后续的http请求丢失在哪里。

Can anyone show me lights to fix this issue? 任何人都可以向我展示灯来解决这个问题吗?

Below is my code for HttpWebRequest call. 下面是我的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);
             }
        }
    }
}

I think i am very late, but i still want to answer your question, may be it can be helpful to others. 我想我已经很晚了,但我仍然想回答你的问题,可能对别人有帮助。 By default HTTP Request you make are HTTP 1.1 requests. 默认情况下,您发出的HTTP请求是HTTP 1.1请求。 And HTTP 1.1 Request by default has Keep-Alive connection. 默认情况下,HTTP 1.1请求具有Keep-Alive连接。 so when you make too many request to same server .net framework only make x no. 所以当你向同一个服务器.net框架提出太多请求时,只需要x不。 of request. 请求。

you should close all your response by response.Close() 你应该通过response.Close()关闭你的所有响应

you can also specify how many simultaneous requests you can make. 您还可以指定可以同时发出的请求数。

ServicePointManager.DefaultConnectionLimit = 20;

Note that you have to set DefaultConnectionLimit before the first request you make. 请注意,您必须在第一个请求之前设置DefaultConnectionLimit you can find more information here on msdn . 你可以在这里找到更多关于msdn的信息。

All i can see is that in GetRequestStreamCallback you should replace 我只能看到你应该替换GetRequestStreamCallback

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

by 通过

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

since these length aren't necessarily equal. 因为这些长度不一定相等。

@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. @Somnath我不确定你是否找到了这个答案,但是如果有其他人在这篇文章中偶然发现Somnath和我有同样的问题。

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

I followed all suggestion provide by all of you but couldn't stop the silent failure of HTTP request. 我遵循了所有人提供的所有建议,但无法阻止HTTP请求的无声失败。 But I found a workaround. 但我找到了一个解决方法。 Even myself could not reach to a final conclusion till now. 甚至直到现在我都无法达成最终结论。

But my workaround is working well as off now without any failure. 但我的解决方法现在运作良好,没有任何失败。

In SendReq(string url) function i have added the following lines of code 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM