WebClient网站登录

时间:2012-06-17 20:56:23

标签: c# login browser webclient webpage

登录网页时遇到困难(http://hurt.prosto.pl);它一直返回到登录页面源...任何想法如何修复它?

var loginData = new NameValueCollection();
loginData.Add("username", username);
loginData.Add("password", password);

var client = new CookieAwareWebClient();
client.BaseAddress = @"http://hurt.prosto.pl/accounts/login/?next=/hurt/";
client.UploadValues("http://hurt.prosto.pl/accounts/login/?next=/hurt/", "POST", loginData);

// now you are logged in and can request pages    
string htmlSource = client.DownloadString("http://hurt.prosto.pl/hurt/myorders/--/category/all1/");
webBrowser2.DocumentText = htmlSource;

这是我的CookieAwereWebClient类:

namespace HTMLParser
{
    internal class CookieAwareWebClient : WebClient
    {
        private CookieContainer cookie = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = cookie;
            }
            return request;
        }
    }
}

2 个答案:

答案 0 :(得分:4)

为像我这样的人解决方案:) glp>

string loginData = "username=***&password=***&next=/hurt/";
            WebClient wc = new WebClient();
            string cookie = string.Empty;
            wc.BaseAddress = @"http://hurt.prosto.pl/accounts/login/";


            wc.Headers.Add("Content-Type: application/x-www-form-urlencoded");
            wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
            wc.Headers.Add("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            wc.Headers.Add("Accept-Encoding: identity");
            wc.Headers.Add("Accept-Language: en-US,en;q=0.8");
            wc.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3");



            wc.UploadString("http://hurt.prosto.pl/accounts/login/", "POST", loginData);
            cookie = wc.ResponseHeaders["Set-Cookie"].ToString();
            wc.Headers.Add("Cookie", cookie);
            wc.UploadString("http://hurt.prosto.pl/accounts/login/", "POST", loginData);
            webBrowser1.DocumentText = wc.DownloadString("http://hurt.prosto.pl/hurt/myorders/--/category/all1/");

答案 1 :(得分:3)

string string loginData = "username=***&passowrd=***&next=/hurt/";

WebClient wc = new WebClient();

wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5");
wc.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
wc.Headers.Add("Accept-Encoding", "identity");
wc.Headers.Add("Accept-Language", "en-US,en;q=0.8");
wc.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
wc.Headers.Add("ContentType", "application/x-www-form-urlencoded");
string response = wc.UploadString("http://hurt.prosto.pl/accounts/login/", "POST", loginData);

饼干:

// general retrieve
string cookie = wc.ResponseHeaders["Set-Cookie"].ToString();

// general send
wc.Headers.Add("Cookie", cookie);