需要帮助,我如何使用HttpWebRequest登录www.shutterstock.com,其他网站是好的
本文关键字:其他 com shutterstock 网站 www 登录 帮助 HttpWebRequest 何使用 | 更新日期: 2023-09-27 18:17:15
我用我的帐户登录www.shutterstock.com在IE或Firefox是好的,但使用HttpWebRequest不能,我已经保存cookie数据在CookieContainer(),我的程序得到cookie是不同的数字与IE浏览器,为什么不,我已经分配请求信息UserAgent,Referer,Host…
谁都能告诉我出了什么问题。我该怎么做才能登录这个网站。以下是我的代码。帐户是虚的,不是真的。 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);
最好的方法是使用Web Browser Object。它会自动从IE获取cookie信息并传递给用户。