HttpWebRequest的停止工作突然,没有任何反应很少请求后收到没有任何、反应、突然、工作

2023-09-04 02:45:17 作者:正宗傻白甜

我正与一个WPF .NET 4.0的应用。我有一个搜索栏。对于每一个搜索令牌,我需要做8 HTTP请求到8个独立的URL来获得搜索结果。我送8请求到服务器后400毫秒,一旦用户在搜索栏中停止输入。搜索6〜7搜索结果令牌来非常漂亮。但在那之后突然的HttpWebRequest停止工作默默。没有异常被抛出,没有收到任何答复。我正在使用Windows 7,我禁用防火墙了。我不知道以后的http请求都将丢失。

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

下面是我的$ C $下HttpWebRequest的来电。

 公共静态无效SendReq(字符串URL)
{
    //创建一个新的HttpWebRequest对象。
    HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(URL);

    request.ContentType =应用/的X WWW的形式urlen codeD;
    request.Proxy =新WebProxy(192.168.1.1,8000);

    //方法属性设置为POST将数据发布到URI。
    request.Method =POST;

    //启动异步操作
    request.BeginGetRequestStream(新的AsyncCallback(GetRequestStreamCallback),请求);

}

私有静态无效GetRequestStreamCallback(IAsyncResult的asynchronousResult)
{
    HttpWebRequest的要求=(HttpWebRequest的)asynchronousResult.AsyncState;

    // END操作
    流postStream = request.EndGetRequestStream(asynchronousResult);

    字符串POSTDATA = this.PostData;

    //将字符串转换成字节数组。
    字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);

    //写入请求流。
    postStream.Write(字节数组,0,byteArray.Length);
    postStream.Close();

    //开始异步操作获得响应
    request.BeginGetResponse(新的AsyncCallback(GetResponseCallback),请求);
}

私有静态无效GetResponseCallback(IAsyncResult的asynchronousResult)
{
    HttpWebRequest的要求=(HttpWebRequest的)asynchronousResult.AsyncState;

    // END操作
    使用(HttpWebResponse响应=(HttpWebResponse)request.EndGetResponse(asynchronousResult))
    {
        使用(流streamResponse = response.GetResponseStream())
        {
             使用(StreamReader的streamRead =新的StreamReader(streamResponse))
             {
                 字符串responseString = streamRead.ReadToEnd();
                 的Debug.WriteLine(responseString);
             }
        }
    }
}
 

解决方案

我觉得我是很晚了,但我还是想回答你的问题,可能是它可以帮助他人。默认情况下HTTP请求你做是HTTP 1.1请求。和HTTP 1.1请求在默认情况下具有保持活动连接。所以当你做太多的要求,同一台服务器的.NET Framework只能使X无。的要求。

您应该附近 response.Close()

您所有的反应 RPC框架的使用场景

您还可以指定有多少你可以同时请求做出。

  ServicePointManager.DefaultConnectionLimit = 20;
 

请注意,您必须设置 DefaultConnectionLimit 你做的第一个请求之前。你可以找到更多信息 这里MSDN上

I’m working with a WPF .net 4.0 Application. I have a search bar. For each search token I need to do 8 http request to 8 separate URLs to get search results. I send 8 requests to server after 400 milliseconds once the user stops typing in search bar. Searching for 6 to 7 search-tokens results comes very nicely. But after that suddenly HttpWebRequest stops working silently. No exception was thrown, no response was received. I'm working with Windows 7, I disabled the firewall too. I don't know where the subsequent http requests are lost.

Can anyone show me lights to fix this issue?

Below is my code for HttpWebRequest call.

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. And HTTP 1.1 Request by default has Keep-Alive connection. so when you make too many request to same server .net framework only make x no. of request.

you should close all your response by 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. you can find more information here on msdn.

 
精彩推荐
图片推荐