[英]System.Net.WebClient with username and password
我有一個WebClient對象,並且正在使用DownloadFileAsync方法來獲取文件,盡管在我需要登錄該站點之前,這似乎很困難。 我想登錄https://ssl.rapidshare.com/ (似乎正在使用Forms身份驗證)。 我發現以下可識別Cookie的WebClient代碼:
public class CookieAwareWebClient:WebClient {
private CookieContainer m_container=new CookieContainer();
protected override WebRequest GetWebRequest(Uri address) {
WebRequest request=base.GetWebRequest(address);
if(request is HttpWebRequest) {
(request as HttpWebRequest).CookieContainer=m_container;
}
return request;
}
}
但是,恐怕我的代碼似乎沒有合作:
gWebClient=new CookieAwareWebClient();
if(gGroupEntry.GetStringProperty(CGroupEntry.EStringProperties.DownloadURL).Contains("rapidshare.com"))
gWebClient.Credentials=new System.Net.NetworkCredential(CSaverLoader.gUsername, CSaverLoader.gPassword, "https://ssl.rapidshare.com/");
gWebClient.DownloadFileCompleted+=new AsyncCompletedEventHandler(gWebClient_DownloadFileCompleted);
gWebClient.DownloadProgressChanged+=new DownloadProgressChangedEventHandler(gWebClient_DownloadProgressChanged);
gWebClient.DownloadFileAsync(new Uri(gGroupEntry.GetStringProperty(CGroupEntry.EStringProperties.DownloadURL)), gDownloadDirectory+"/"+gGroupEntry.GetStringProperty(CGroupEntry.EStringProperties.FileName));
為什么會這樣呢?
您是否可以在System.Net
命名空間中使用HttpWebRequest類 ? 與用於發出HTTP請求的輕量級WebClient
相比,這是一個靈活得多的類,它包含諸如HttpWebRequest.CookieContainer Property之類的東西。
如果網站使用基於cookie的身份驗證而不是HTTP基本身份驗證,則需要將cookie放入CookieContainer中,而不是設置憑據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.