【发布时间】:2017-02-04 15:07:02
【问题描述】:
对不起,我的英语不好。我每 10 秒使用一次 HttpWebRequest。但有时,响应是等待很长时间。 10秒后,下一个过程来了。因此,请求被堆叠起来。由于公司在总部采取的措施,我被禁止了,因为之前的连接还没有关闭。我设置了请求的属性,如 Timeout、ContinueTimeout、ReadWriteTimeout,但结果没有改变。实际上,它属于异常。但是当我观看“Charles Proxy”时,请求仍在等待响应。但我必须以某种方式阻止它。我分享我的代码和查尔斯的照片。谢谢你的回答。最好的问候。
string jsondata = "";
const string url = "https://website.com";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
NameValueCollection header_items = GetHeaders();
req.Headers.Add(header_items);
req.Method = "POST";
req.Host = "website.com";
req.Accept = "application/json, text/plain, */*";
req.ContentType = "application/json;charset=utf-8";
req.KeepAlive = false;
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)";
req.Referer = "https://website.com";
req.AllowAutoRedirect = false;
req.ContinueTimeout = 5000;
req.ReadWriteTimeout = 5000;
req.Timeout = 5000;
req.ContentLength = 0;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
ServicePointManager.DefaultConnectionLimit = 500;
ServicePointManager.DnsRefreshTimeout = 0;
ServicePointManager.MaxServicePointIdleTime = 5000;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
try
{
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
jsondata = reader.ReadToEnd();
reader.Close();
}
response.Close();
}
if (jsondata.Length > 200)
{
//Do Something
}
}
catch (WebException ex)
{
req.Abort();
//Do Something
}
catch (Exception ex)
{
req.Abort();
//Do Something
}
【问题讨论】:
标签: c# asp.net httpwebrequest httpwebresponse connection-timeout