使用Webclient下载文件显示错误的数据 [英] Download file using Webclient shows Wrong Data

查看:70
本文介绍了使用Webclient下载文件显示错误的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我使用以下代码从安全网站下载文件,但在文件夹@C:\Downloads\Filename上创建的文件显示HTML这样的标签



Hi Guys,

I used following code to download file from secured site but file created on folder @"C:\Downloads\Filename" show HTML tags like this

<HTML>
<HEAD><LINK rel="stylesheet" type="text/css" name="defaultstyle" href="/cehttp/html/style.css">
<TITLE>Servlet Response Message</TITLE></HEAD>
<BODY>
<H2 align=center><H2>
<HR>
You are not logged on. Please logon first.
<HR>
</BODY></HTML>





而不是实际的文件内容。





instead of the actual file content.

try
       {
           WebClient client = new WebClient();
           client.Credentials = new System.Net.NetworkCredential("username", "password");
           byte[] pageData = client.DownloadData("url");
           string pageHtml = Encoding.ASCII.GetString(pageData);
           Console.WriteLine(pageHtml);

           client.DownloadFile("url", @"C:\Downloads\071213.ACCT");
       }
       catch (WebException webEx)
       {
           Console.WriteLine(webEx.ToString());
           if (webEx.Status == WebExceptionStatus.ConnectFailure)
           {
               Console.WriteLine("Are you behind a firewall?  If so, go through the proxy server.");
           }
       }

推荐答案

这是服务器端的一些机制:他们以某种方式检查您已登录,并且您的用户名和密码不够用。我只能猜测他们可能会使用cookies,检查引用,检查会话变量,......
That's some mechanism on the server side: they check somehow that you are logged in, and your username ans password are not enough. I can only guess that they might use cookies, check referers, check session variables,...


这篇关于使用Webclient下载文件显示错误的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆