一个示例站点是: http: //www.dotmed.com。登录页面位于:https ://secure.dotmed.com/login.html (由 SSL HTTPS 保护)。当您进行身份验证时,您会被重定向到: http: //www.dotmed.com/index.html(不受 SSL 保护)。
我正在使用以前帖子中的代码,但“会话”似乎并没有在请求中持续存在。也许这与在一个域中设置的 cookie 并试图访问另一个域中的页面有关?没有把握。
public static void Login()
{
try
{
CookieAwareWebClient client = new CookieAwareWebClient();
client.BaseAddress = "https://secure.dotmed.com/";
// Do a simple GET on the Login page first (It sets cookies)
string source = client.DownloadString("login.html");
// Attempt a POST to the Login page
NameValueCollection loginData = new NameValueCollection();
loginData.Add("refer", "");
loginData.Add("backfromssl", "1");
loginData.Add("user", "*****************");
loginData.Add("pass", "*********");
byte[] ret = client.UploadValues(LOGIN_PAGE, "POST", loginData);
string pageSource = Encoding.ASCII.GetString(ret); // I can see the user is logged in successfully here
// Now you are logged in and can request pages
client.BaseAddress = "http://www.dotmed.com"; // ???????????????
string htmlSource = client.DownloadString("index.html"); // ???????????????
}
catch { }
}
我的 CookieAwareWebClient 类直接来自:https ://stackoverflow.com/a/14907201/787794
当我尝试使用问号旁边的 client.DownloadString() 方法时,它会返回一个用户未登录的页面。如何使身份验证跨请求保持不变?