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

c# – HttpWebRequest突然停止工作,几个请求后没有收到响应,第1张

概述我正在使用 WPF .net 4.0应用程序.我有一个搜索栏.对于每个搜索令牌,我需要对8个单独的URL执行8个http请求以获取搜索结果.一旦用户停止在搜索栏中输入,我会在400毫秒后向服务器发送8个请求.搜索6到7个搜索令牌的结果非常好.但在那之后突然HttpWebRequest停止了默默工作.没有例外,没有收到任何回复.我正在使用 Windows 7,我也禁用了防火墙.我不知道后续的http 我正在使用 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,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);             }        }    }}
解决方法 我想我已经很晚了,但我仍然想回答你的问题,可能对别人有帮助.默认情况下,您发出的http请求是http 1.1请求.默认情况下,http 1.1请求具有Keep-Alive连接.所以当你向同一个服务器.net框架提出太多请求时,只需要x不.请求.

你应该通过response.Close()关闭你的所有响应

您还可以指定可以同时发出的请求数.

ServicePointManager.DefaultConnectionlimit = 20;

请注意,您必须在第一个请求之前设置DefaultConnectionlimit.你可以找到更多信息
here on msdn.

总结

以上是内存溢出为你收集整理的c# – HttpWebRequest突然停止工作,几个请求后没有收到响应全部内容,希望文章能够帮你解决c# – HttpWebRequest突然停止工作,几个请求后没有收到响应所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1251359.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存