1. 程式人生 > >C#同時非同步發起兩個HttpWebRequest卡死

C#同時非同步發起兩個HttpWebRequest卡死

        private static HttpWebRequest CreatePostHttpWebRequest(string url, string postData)
        {
            var postRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            postRequest.KeepAlive = false;
            postRequest.Timeout = 5000;
            postRequest.Method = "POST";
            postRequest.ContentType = "application/x-www-form-urlencoded";
            postRequest.ContentLength = postData.Length;
            postRequest.AllowWriteStreamBuffering = false;
            StreamWriter writer = new StreamWriter(postRequest.GetRequestStream(), Encoding.ASCII);
            writer.Write(postData);
            writer.Flush();
            return postRequest;
        }

        private static string GetHttpResponse(HttpWebResponse response, string requestType)
        {
            var responseResult = "";
            const string post = "POST";
            string encoding = "UTF-8";
            if (string.Equals(requestType, post, StringComparison.OrdinalIgnoreCase))
            {
                encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8";
                }
            }
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
            {
                responseResult = reader.ReadToEnd();
            }
            return responseResult;
        }

        public static async void HttpPostRequestAsync(string url, string postData, handleAsync ha = null)
        {
            string strPostReponse = string.Empty;
            try
            {
                HttpWebRequest postRequest = CreateHttpRequest(url, "POST", postData);
                HttpWebResponse postResponse = await postRequest.GetResponseAsync() as HttpWebResponse;
                strPostReponse = GetHttpResponse(postResponse, "POST");

                postRequest.Abort();
                postResponse.Close();
            }
            catch (Exception ex)
            {
                strPostReponse = ex.Message;
            }
            if (strPostReponse != "true")
            {
                Console.WriteLine(url + "--> reslut : " + strPostReponse);
                Console.WriteLine(postData);

                if (null != ha)
                {
                    ha(strPostReponse);
                }
            }
            
        }

當外部連續呼叫兩非同步post請求時,介面卡死,請求成功後的Console.WriteLine中沒有任何輸出,分開呼叫是沒問題的,可見是同時連線的問題,正好查詢到一篇文章https://www.crifan.com/fixed_problem_sometime_httpwebrequest_getresponse_timeout/

於是修改建立連線的地方CreatePostHttpWebRequest,加入System.Net.ServicePointManager.DefaultConnectionLimit = 50;


        private static HttpWebRequest CreatePostHttpWebRequest(string url, string postData)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 50;

            var postRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            postRequest.KeepAlive = false;
            postRequest.Timeout = 5000;
            postRequest.Method = "POST";
            postRequest.ContentType = "application/x-www-form-urlencoded";
            postRequest.ContentLength = postData.Length;
            postRequest.AllowWriteStreamBuffering = false;
            StreamWriter writer = new StreamWriter(postRequest.GetRequestStream(), Encoding.ASCII);
            writer.Write(postData);
            writer.Flush();
            return postRequest;
        }

果然不卡死了