3

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 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?

UPDATE

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;
    }
}

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);
                    }

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.

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?

4

1 回答 1

0

(在问题编辑中回答。转换为社区 wiki 答案。请参阅没有答案的问题,但问题在评论中解决(或在聊天中扩展)

OP写道:

解决了。所以问题出在我的耳朵之间。我将安全资源的 URL 传递给 .UploadValues() 方法,知道它会重定向到登录页面。但是,我真的需要从登录表单(提交时)传递 URL,而不是登录页面本身。一旦我这样做了,它就可以正常工作。我想我现在要去食品服务行业找份工作了。

链接

在 SO 上已经发布了一些问题来解决这个问题。一开始我只是不知道我在寻找什么,所以我没有看到那些......这里有一些很好的资源,我在处理这个问题时遇到了:

如何在asp.net中的两个Url之间维护cookie

尝试使用 HttpWebRequest 获取身份验证 cookie

于 2015-02-03T16:59:54.527 回答