我正在尝试使用HttpWebRequest,需要使用SSL。它确实适用于某些网站(例如PayPal),但不是我真正希望它工作的网站,例如this社区网站(URL也在代码示例中)。为什么是这样?证书(我假设这是问题可能出现的地方)看起来很糟糕。
public static bool AcceptAllCertificatePolicy(object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}
static void LoginTest()
{
ServicePointManager.ServerCertificateValidationCallback += AcceptAllCertificatePolicy;
HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("https://steamcommunity.com");
req.Method = "GET";
req.CookieContainer = new CookieContainer();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine("Cookies:" + resp.Cookies.Count + "\r\n" + resp.ResponseUri);
Console.WriteLine(resp.Headers);
resp.Close();
}
由于
编辑:网址不正确,抱歉。问题仍然存在。
EDIT2: 我忘了提到虽然它没有给我任何错误,但它也没有返回任何cookie。这就是我真正追求的。 www.paypal.com给了我7个cookie,响应标题是
Pragma: no-cache
Cache-Control: private
Date: Sat, 31 Oct 2009 12:10:29 GMT
Expires: Thu, 05 Jan 1995 22:00:00 GMT
Set-Cookie: LANG=sv_SE%3bSE; expires=Tue, 29-Oct-2019 12:10:30 GMT; domain=.paypal.com; path=/,cookie_check=yes; expires=Tue, 29-Oct-2019 12:10:30 GMT; domain=.paypal.com; path=/,navcmd=_home-general; domain=.paypal.com; path=/,consumer_display=USER_HOMEPAGE%3d0%26USER_TARGETPAGE%3d0%26USER_FILTER_CHOICE%3d7%26BALANCE_MODULE_STATE%3d1%26GIFT_BALANCE_MODULE_STATE%3d1%26LAST_SELECTED_ALIAS_ID%3d0; expires=Sun, 31-Oct-2010 12:10:30 GMT; domain=.paypal.com; path=/,cwrClyrK4LoCV1fydGbAxiNL6iG=_Yi2Io2xQuP5-kmjAhh1KUJVMH6VTFfnf1Zlw1ONf41yAAUyLXUiLZ_9GJcPZmmNMl6kgpaG14eNnVA6jYlkyaK7h2IPqoPUsUobSg-gzEf4NsHrbwWzuCe8W50EIPW4ABaAkG%7c3EWGI8_elkol97q63exWWZMOA02gaDHo4_Y_7lKC_xHE2kWR8DZGXAmqs4uv075KRh6OW0%7ctJQpXroB_JJPd9EvK7Xx17DfiF9lz5vs06ThWWluWxwM3-WO82KChgIGdC080wwm5Q6vL0%7c1256991030; domain=.paypal.com; path=/,navlns=0.0; expires=Fri, 26-Oct-2029 12:10:30 GMT; domain=.paypal.com; path=/,Apache=10.190.11.252.1256991029926042; path=/; expires=Fri, 18-Sep-03 05:42:13 GMT
Server: Apache
Vary: Accept-Encoding
Strict-Transport-Security: max-age=500
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
而steamcommunity.com导致设置了0个cookie,
Pragma: no-cache
Connection: close
Transfer-Encoding: chunked
Cache-Control: no-cache
Content-Type: text/html; charset=UTF-8
Date: Sat, 31 Oct 2009 12:14:25 GMT
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Server: Apache
即使它应该设置至少一个。
答案 0 :(得分:1)
为firefox安装firebug。清除Cookie缓存,然后导航到该网站。检查firebug以查看浏览器发送的确切请求。然后使用代码复制完全相同的请求。这意味着复制浏览器发送的所有标头。
如果某些服务器认为它不是发出请求的浏览器,则它们不会发送cookie。因此,放置所有请求标头应使其在网站上显示为浏览器正在请求页面。
答案 1 :(得分:0)
问题是证书对https://www.steamcommunity.com
无效,但对https://steamcommunity.com
无效。所以你要做的就是改变你的网址:
WebRequest.Create("https://steamcommunity.com");
查看Issued to
标题。对于paypal,它是Issued to: www.paypal.com
,而对于您的网站,它是Issued to: steamcommunity.com
更新:
new WebClient().DownloadString("https://steamcommunity.com"); // works
new WebClient().DownloadString("https://www.steamcommunity.com"); // throws invalid certificate