我用我的账号在 IE 或 Firefox 中登录 www.shutterstock.com 没问题,但是使用 HttpWebRequest 不行,我已经在 CookieContainer() 中保存了 cookie 数据,我的 Programe Get cookie 与 IE 浏览器的编号不同,为什么不呢,我已分配请求信息 UserAgent,Referer,Host ...
任何人都可以告诉我有什么问题。我该怎么做才能登录这个 web.following 是我的代码。账户是虚拟的,不是真的。
string session_id = "";
string str_key = "";
string str_value = "";
string url = "";
url = "http://www.shutterstock.com/";
CookieContainer cookies = new CookieContainer();
//////////////////////////////////////////////////////////////////////////
// first, request the login form to get the viewstate value
HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
webRequest.MediaType = "GET";
webRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
webRequest.CookieContainer = cookies;
webRequest.Referer = "";
webRequest.Host = "www.shutterstock.com";
webRequest.Accept = "text/html, application/xhtml+xml, */*";
webRequest.KeepAlive = true;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
OutputCookieData(url, response);
foreach (Cookie cookie_object in response.Cookies)
{
str_key = HttpUtility.UrlDecode(cookie_object.Name);
str_value = HttpUtility.UrlDecode(cookie_object.Value);
if (str_key == "ssssidd")
{
session_id = str_value;
break;
}
//str_cookie += str_key + "=" + str_value + Environment.NewLine;
}
string responseData = responseReader.ReadToEnd();
responseReader.Close();
WriteDayLog(responseData);
// extract the viewstate value and build out POST data
string postData =
String.Format(
"user=susimage&pass=110205&session_id={0}",
session_id
);
//////////////////////////////////////////////////////////////////////////
// have a cookie container ready to receive the forms auth cookie
// now post to the login form
webRequest = WebRequest.Create("https://secure.shutterstock.com/login.mhtml") as HttpWebRequest;
webRequest.Method = "POST";
webRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;
webRequest.KeepAlive = true;
webRequest.Referer = "http://www.shutterstock.com/";
webRequest.Host = "secure.shutterstock.com";
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
// we don't need the contents of the response, just the cookie it issues
response = (HttpWebResponse)webRequest.GetResponse();
responseReader = new StreamReader(response.GetResponseStream());
OutputCookieData("https://secure.shutterstock.com/login.mhtml", response);
responseData = responseReader.ReadToEnd();
responseReader.Close();
WriteDayLog(responseData);