任何人都可以向我展示灯来解决这个问题吗?
下面是我的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突然停止工作,几个请求后没有收到响应所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)