繁体   English   中英

需要帮助,我该如何使用HttpWebRequest登录www.shutterstock.com,其他网站还可以

[英]Need help,How can i use HttpWebRequest login www.shutterstock.com,Other website is fine

我使用我的帐户在IE或Firefox中登录www.shutterstock.com很好,但是不能使用HttpWebRequest,我已将Cookie数据保存在CookieContainer()中,我的程序与IE浏览器的Cookie获取号不同,为什么不这样做,我已分配请求信息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浏览器对象 它将自动从IE获取cookie信息,并将其传递出去。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM