问题描述
我正在尝试使用用户名/密码从网站下载文件.您需要为注册文件付费才能下载文件-我们已经完成了.我正在尝试传递用户名/密码并下载文件,如下所示:
I am trying to download files from a website with username/password. You need to pay for a registered account in order to download files - which we have done. I am attempting to pass in the username/password and download a file as follows:
if (docUrl != null)
{
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
this.WebClientInstance.Credentials = new NetworkCredential(username, password);
fileData = this.WebClientInstance.DownloadData(docUrl);
this.WebClientInstance.Dispose();
isDataDownloaded = true;
}
WebClientInstance是一个System.Net.WebClient.我调试并验证它是否可以设置凭据.我没有下载PDF,而是获得了一个HTML页面,该页面提示我登录以访问文件.我已验证用户名/密码正确.我使用相同的凭据通过WatiN抓取该网站.
WebClientInstance is a System.Net.WebClient. I debugged and verified that it is hitting the line to set credentials. Instead of downloading the PDF, I end up with an HTML page that prompts me to log in to get access to the file. I have verified that the username/password is correct. I use the same credentials to scrape the website with WatiN.
我还要在这里做什么吗?
Is there something else that I'm supposed to be doing here?
更新
好的,我已经进行了一些嗅探,并找到了一些有关此问题的有用信息.我仍然没有使它起作用,但是我想我离得更近了.首先,您需要创建一个支持cookie的WebClient,以扩展WebClient类,如下所示:
Okay, I've done some sniffing around and found some useful info on this issue. I still haven't gotten it to work, but I think I'm closer. First, you need to create a cookie aware WebClient that extends the WebClient class, as follows:
public class CookiesAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; private set; }
public CookiesAwareWebClient()
{
this.CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var webRequest = base.GetWebRequest(address);
if (webRequest is HttpWebRequest)
(webRequest as HttpWebRequest).CookieContainer = this.CookieContainer;
return webRequest;
}
}
下一步是使用WebClient.UploadValues()方法将登录信息上载到目标网站.认证和下载目标资源的完整过程如下:
Next is to use the WebClient.UploadValues() method to upload the login info to the target website. The full process of authenticating and downloading the target resource is as follows:
using (var webClient = new CookiesAwareWebClient())
{
var postData = new NameValueCollection()
{
{ "userId", username },
{ "password", password }
};
webClient.UploadValues(docUrl, postData);
fileData = webClient.DownloadData(docUrl);
}
对于使用表单身份验证的网站,我错了.这是一个JSP网站,并使用JSESSIONID.我已验证自己是否正在返回cookie,其中包含似乎是有效的32字节JSESSIONID值.
I was wrong about the site using forms auth. It is a JSP website and uses a JSESSIONID. I have verified that I am getting a cookie back with what appears to be a valid 32-byte JSESSIONID value.
但是,当我调用WebClient.DownloadData()时,它仍然仅返回重定向的登录页面.我试图通过将HttpWebRequest上的AllowAutoRedirect属性设置为false来解决此问题,但是它返回0个字节.
However, when I call WebClient.DownloadData() it is still only returning the redirected login page. I've tried to fix this by setting the AllowAutoRedirect property on the HttpWebRequest to false, but then it returns 0 bytes.
我还有其他需要做的事情,它不会重定向,并且在我通过身份验证后将带我到资源中吗?
Is there something else that I need to do so it won't redirect and will take me to the resource once I have authenticated?
推荐答案
(在问题编辑中回答.转换为社区Wiki答案.请参见)
(Answered in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
OP写道:
链接
SO上已经发布了一些解决此问题的问题.起初我只是不知道自己在寻找什么,所以我没有看到那些……在解决此问题时,我在这里遇到了很多不错的资源:
There were already a few questions posted on SO that addressed this issue. I just didn't know what I was looking for at first so I didn't see those... Anywhere here are a couple good resources that I came across when working on this issue:
这篇关于具有凭据的WebClient仍未下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!