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
}
答案 0 :(得分:0)
尝试为您的请求设置代理设置。很多时候查找代理需要记录HttpWebRequest
:
req.Proxy = GetSystemWebProxy();
另请看一下:https://en.code-bude.net/2013/01/21/3-things-you-should-know-to-speed-up-httpwebrequest/