[英]Virtual user XXX Logged in while downloading file
I am using WebClient class for downloading files from a given URL.我正在使用 WebClient class 从给定的 URL 下载文件。 I am using following function to save it on local server,
我正在使用以下 function 将其保存在本地服务器上,
WebClient wb = new WebClient();
NetworkCredential creds = new NetworkCredential("my username", "my password");
IWebProxy proxy = HttpWebRequest.DefaultWebProxy;
wb.Proxy = proxy;
wb.Credentials = creds;
wb.DownloadFile("source url", "C://downloaded.rpt.rsp");
but after processing destination file contains但在处理目标文件后包含
Virtual user XXX logged in successfully
虚拟用户 XXX 登录成功
Your code seems correct.您的代码似乎正确。 Did you try to manually download that file via a web-browser (like IE or chrome) and see what it downloads?
您是否尝试通过网络浏览器(如 IE 或 chrome)手动下载该文件并查看它下载的内容? If it still gets that, maybe the server is re-directing the URL to somewhere.
如果它仍然得到那个,也许服务器正在将 URL 重定向到某个地方。 plz check.
请检查。
Just for your reference, the minimal code (without auth and proxy) to download a file.仅供您参考,下载文件的最小代码(没有身份验证和代理)。
using (WebClient Client = new WebClient ())
{
Client.DownloadFile("http://www.abc.com/file/song/a.mpeg", "a.mpeg");
}
It seems like 2 step response from the server side.似乎来自服务器端的两步响应。 Authentication (with cookie) and redirect to the file.
身份验证(使用 cookie)并重定向到文件。
You can see the details when downloading with the browser with a Fiddler on.您可以在打开 Fiddler 的浏览器下载时查看详细信息。 The browser does a redirect and that won't work when doing Http request.
浏览器执行重定向,并且在执行 Http 请求时不起作用。
Try requesting with httpWebRequest and cookie container.尝试使用 httpWebRequest 和 cookie 容器进行请求。 And call it again with the authentication cookie.
并使用身份验证 cookie 再次调用它。
I think you need to do two steps, 1) login, 2) download file.我认为您需要执行两个步骤,1)登录,2)下载文件。
Try this:尝试这个:
CookieAwareWebClient wb = new CookieAwareWebClient();
string response = wb.DownloadString("source url"); //should say Virtual user XXX Logged in
wb.DownloadFile("source url", "C://downloaded.rpt.rsp");
Be sure you are using CookieAwareWebClient:确保您使用的是 CookieAwareWebClient:
public class CookieAwareWebClient : WebClient
{
private readonly CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest = request as HttpWebRequest;
if (webRequest != null)
{
webRequest.CookieContainer = m_container;
}
return request;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.