c# - WebClient网站登录

标签 c# authentication browser webclient html

我无法登录网页 (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;
        }
    }
}

最佳答案

为像我这样的人提供解决方案:) gl

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/");

关于c# - WebClient网站登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11074731/

相关文章:

ruby - 避免使用相同的用户名和密码多次登录

html - 页面(内容)呈现后呈现字体

java - 启动Firefox并等待其关闭

C# 方法返回连接到不同数据库

C#/C++ - 如何获取路径太长或权限被拒绝的目录的大小?

c# - 如何将图像添加到 DataGridView 中的单个特定单元格?

c# - 如何找到可用的 COM 端口?

javascript - Facebook 登录按钮未显示在我的页面上

cookies - Google 如何知道我仍处于登录状态?

java - 如何让Java驱动/控制webkit/gecko?