windows 7 C# HttpWebRequest第一次请求很慢超时的原因?

时间:2024-12-13 07:25:35
private const int _timeOut = 15000; public static int TimeOut = 120000; public static int TimeOutReadWrite = 15000; public string PostNew(string url, string data, string strAuthorization = null, string tenant = null, bool IsSaas = false) { string ReadToEnd = ""; try { label1.Text = DateTime.Now.ToString(); System.GC.Collect(); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => { return true; //总是接受 }); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls; //|(SecurityProtocolType)0x300; //| (SecurityProtocolType)0xC00; //Tls12 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/json;charset=utf-8";//"application/json" // = false; request.Timeout = TimeOut; request.ReadWriteTimeout = TimeOutReadWrite; request.ServicePoint.Expect100Continue = false; System.Net.ServicePointManager.DefaultConnectionLimit = 65500; request.Proxy = null; request.AllowWriteStreamBuffering = false; request.ServicePoint.UseNagleAlgorithm = false; //if (!(strAuthorization) && IsSaas) // (, "Bearer " + strAuthorization); //if (!(tenant) && IsSaas) // (_tenant, tenant); byte[] byteData = System.Text.Encoding.UTF8.GetBytes(data.ToString()); request.ContentLength = byteData.Length; request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } //响应 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); ReadToEnd = reader.ReadToEnd(); } label2.Text = DateTime.Now.ToString(); } catch (Exception ex) { throw new ApplicationException("连接服务器发生异常(#" + ex.Message + ")"); } return ReadToEnd; }