我的朋友正在使用C#编写一个请求网页的简单程序。然而,当他试图请求指定网页时,他遇到了问题。他已经尝试在请求中设置所有的头和cookie,但仍然得到超时异常。示例网页为https://my.ooma.com
代码如下:
string url = "https://my.ooma.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 30000;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5";
request.Method = "GET";
request.CookieContainer = new CookieContainer();
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Add("Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3");
request.Headers.Add("Accept-Encoding:gzip,deflate,sdch");
request.Headers.Add("Accept-Language:en-US,en;q=0.8");
request.KeepAlive = true;
WebResponse myResponse = request.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream());
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
所有头部与使用Chrome浏览网页时相同。他也没有看到任何使用Chrome开发者工具设置的cookie。有没有人可以使用C#成功地请求页面?非常感谢。
发布于 2012-05-31 11:52:21
抱歉来迟了。下面的代码片段应该可以很好地工作。我也尝试了有"getodds.xgi“在其中的旅游旧网址,它也工作得很好。对于仅使用安全超文本传输协议(HTTPS)方案的连接,服务器使用安全套接字层(SSL)协议。
如果它们只是为了获得响应,则不需要设置任何UserAgent或Header。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
WebRequest request = WebRequest.Create("http://my.ooma.com/");
string htmlResponse = string.Empty;
using (WebResponse response = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
htmlResponse = reader.ReadToEnd();
reader.Close();
}
response.Close();
}
https://stackoverflow.com/questions/10780153
复制相似问题