gpt4 book ai didi

credentials - 具有凭据的 WebClient 仍未下载文件

转载 作者:行者123 更新时间:2023-12-04 23:51:41 26 4
gpt4 key购买 nike

我正在尝试使用用户名/密码从网站下载文件。您需要为注册帐户付费才能下载文件 - 我们已经做到了。我正在尝试传递用户名/密码并按如下方式下载文件:

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 一起抓取网站。

还有什么我应该在这里做的吗?

更新

好的,我四处搜寻了一下,找到了一些关于这个问题的有用信息。我还没有让它工作,但我想我更接近了。首先,您需要创建一个扩展 WebClient 类的 cookie 感知 WebClient,如下所示:

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()方法将登录信息上传到目标网站。目标资源鉴权和下载的完整流程如下:

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 值。

但是,当我调用 WebClient.DownloadData() 时,它仍然只返回重定向的登录页面。我试图通过将 HttpWebRequest 上的 AllowAutoRedirect 属性设置为 false 来解决此问题,但随后它返回 0 个字节。

还有什么我需要做的,这样它就不会重定向,并且会在我通过身份验证后将我带到该资源吗?

最佳答案

(在问题编辑中回答。转换为社区维基答案。参见 Question with no answers, but issue solved in the comments (or extended in chat))

OP 写道:

Solved. So the problem was between my ears. I was passing in the URL for the secure resource to the .UploadValues() method, knowing that it would redirect to the login page. However, I really needed to pass in the URL from the login form (where it goes upon submitting) - not the login page itself. Once I did that, it worked correctly. I think I'm going to go find a career in food service now.

LINKS

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:

how to maintaine cookies in between two Url's in asp.net

Trying to get authentication cookie(s) using HttpWebRequest

关于credentials - 具有凭据的 WebClient 仍未下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com