【发布时间】:2011-06-16 00:31:17
【问题描述】:
我有一个 WebClient 对象,并且正在使用 DownloadFileAsync 方法获取文件,但在此之前我需要登录到该站点,这似乎很困难。我想登录https://ssl.rapidshare.com/(似乎正在使用表单身份验证)。我发现以下可识别 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));
为什么会这样?
【问题讨论】:
标签: c# cookies login webclient