在c#“System.Net.NetworkCredential”中没有超载的webclient进入网页身份验证

时间:2013-03-11 10:10:19

标签: c# authentication web-scraping webclient

我正在尝试连接和下载需要非“超载”身份验证的网站内容。

选项包括:

  • 用户
  • 用户+密码,
  • 用户+密码+域

我需要用户+订阅者+密码。我怎样才能通过这个凭证?

代码:

WebClient client = new WebClient();  
public void search()  
{  
        client.Credentials = new System.Net.NetworkCredential(username, subscriber, password); //not really exist  
        Byte[] pageData = client.DownloadData("https://example.com/");
...  
}

1 个答案:

答案 0 :(得分:1)

我是如何解决问题的: 使用了带有ImprovedWebClient类的POST方法,就像webclient一样可以保存cookie。你可以找到它here 代码:

ImprovedWebClient wc = new ImprovedWebClient();
wc.DownloadData("https://theWebSite.com"); //to enter home page and get cookie
string URI = "https://theWebSite.com/authenticate"; //the login page
string myParameters = "user=theUserName&subscriber_number=1234-5678&password=myPassword&commit=LogIn"; //the parameters for the website
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.UploadString(URI, myParameters);

感谢帮助人员。