C# 尝试使用httpwebrequest从SSL网站获取数据时超时?
长话短说,我正试图通过httpwebrequest访问,下面的代码在say或我们公司的网站上有效,但在Yahoo或Nasdaq上无效 这是带有注释的代码,因此它是自描述性的,提前谢谢C# 尝试使用httpwebrequest从SSL网站获取数据时超时?,c#,.net,C#,.net,长话短说,我正试图通过httpwebrequest访问,下面的代码在say或我们公司的网站上有效,但在Yahoo或Nasdaq上无效 这是带有注释的代码,因此它是自描述性的,提前谢谢 // I've tried with tls12 or ssl3, still no luck System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; // "bumb" is just the var
// I've tried with tls12 or ssl3, still no luck
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
// "bumb" is just the variable storing the web address
HttpWebRequest requestHtm = (HttpWebRequest)WebRequest.Create(bumb);
// just in case...
ServicePointManager.ServerCertificateValidationCallback = delegate { return
true; };
//below follow a few lines trying to impersonate browser...just in case
requestHtm.CookieContainer = new CookieContainer();
requestHtm.AllowAutoRedirect = true;
requestHtm.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
requestHtm.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
requestHtm.Headers[HttpRequestHeader.AcceptLanguage] = "en-gb,en;q=0.5";
requestHtm.Headers[HttpRequestHeader.AcceptCharset] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
requestHtm.Timeout = 15000;
requestHtm.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
requestHtm.Method = "GET";
// using "using" for simplicity...
using (HttpWebResponse responseHtm = (HttpWebResponse)requestHtm.GetResponse())
{
StreamReader r = new StreamReader(responseHtm.GetResponseStream());
string text1 = r.ReadToEnd();
//everything fails here: no messsage box gets shown, just timeout error
MessageBox.Show(text1);
} //closing the "using"
顺便说一句:这是针对4.5的,我正在win 8上测试,可能会在win 2012服务器上尝试,但需要它普遍工作…在4.0上,第一行不工作,但有办法替代Lambda exp。看起来,在任何框架版本上仍然没有运气。如果您将
15000
更改为150000
,会发生什么情况?^现在将尝试它…显然我还需要等待更多:)@mjwills:看起来您是对的!我只是在雅虎上尝试了更长的超时时间,它工作正常……但是在纳斯达克,当我通过谷歌chrome打开它时,它只是不断地“旋转”……我指的是网站负载,但不是“完全”…aparently NET等待页面停止加载,但Nasdaq通过jscript或其他方式不断拉取实时数据,导致超时…这也是我的意思,但我的意思是,我甚至无法从chrome在Nasdaq上加载页面,因此它甚至可能是我的internet连接,ps-如果你可以添加它作为答案,以便我可以接受,10倍。
/// button + textbox are added to the windows form, you also need to add
///Using System.IO; Using System.Net; Using System.Net.Security at the very beginning...
private void button1_Click(object sender, EventArgs e)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11;
ServicePointManager.Expect100Continue = true;
string bumb = "https://nasdaq.com";
HttpWebRequest requestHtm = (HttpWebRequest)WebRequest.Create(bumb);
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
// requestHtm.CookieContainer = new CookieContainer();
requestHtm.AllowAutoRedirect = true;
requestHtm.KeepAlive = true;
// requestHtm.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
// requestHtm.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
// requestHtm.Headers[HttpRequestHeader.AcceptLanguage] = "en-gb,en;q=0.5";
// requestHtm.Headers[HttpRequestHeader.AcceptCharset] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
requestHtm.Timeout = 150000;
//requestHtm.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
requestHtm.UserAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36";
//requestHtm.Method = "GET";
using (HttpWebResponse responseHtm = (HttpWebResponse)requestHtm.GetResponse())
{
StreamReader r = new StreamReader(responseHtm.GetResponseStream());
string text1 = r.ReadToEnd();
textBox1.Text = text1;
}
}