【问题标题】:C# : webclient downloads the source code of a html page instead of the actual resourceC#:webclient 下载 html 页面的源代码而不是实际资源
【发布时间】:2011-06-10 17:34:59
【问题描述】:

我从一个 MSDN 博客中采用了这段代码,并添加了 webclient 来下载资源..

string formUrl = "My login url";
            string formParams = string.Format("userName={0}&password={1}&x={2}&y={3}&login={4}", "user", "password","0","0","login");
            string cookieHeader;
            WebRequest req = WebRequest.Create(formUrl);
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(formParams);
            req.ContentLength = bytes.Length;
            using (Stream os = req.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length);
            }
            WebResponse resp = req.GetResponse();
            cookieHeader = resp.Headers["Set-cookie"];
            string pageSource;
            string getUrl = "Resource url";
            WebRequest getRequest = WebRequest.Create(getUrl);
            getRequest.Headers.Add("Cookie", cookieHeader);
            WebResponse getResponse = getRequest.GetResponse();
            using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
            {
                pageSource = sr.ReadToEnd();
                System.Console.WriteLine(sr.ToString());
            }

            WebClient wc = new WebClient();
            wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";

            wc.DownloadFile("Resource url","C:\\abc.tgz");
            Console.Read();

但是 abc.tgz 并不是它应该的样子。因此,当我使用记事本打开它时,我注意到它是“我的登录 URL”页面的源文件.. 我哪里错了?

我可以使用 webclient 的任何属性来查看错误 .. 即 .. 基地址等吗?

【问题讨论】:

  • 你确定所有表单域的名称都完全正确吗?
  • 它不是在下载“源代码”——它是在为登录页面提供 HTML 服务;就这些。在大多数情况下,像这样的安全页面不适合抓取。您已检查这是否包含在网站条款和条件中是吗?
  • getUrlformUrl 不同。它们应该是两个单独的请求吗?
  • 听起来你的代码正在做它应该做的事情。它正在下载它被重定向到的页面。

标签: c# webclient


【解决方案1】:

让我们让事情变得更简单,好吗:

public class CookiesAwareWebClient : WebClient
{
    public CookieContainer CookieContainer { get; private set; }

    public CookiesAwareWebClient()
    {
        CookieContainer = new CookieContainer();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        ((HttpWebRequest)request).CookieContainer = CookieContainer;
        return request;
    }
}

class Program
{
    static void Main()
    {
        using (var client = new CookiesAwareWebClient())
        {
            var values = new NameValueCollection
            {
                { "userName", "user" },
                { "password", "password" },
                { "x", "0" }, // <- I doubt the server cares about the x position of where the user clicked on the image submit button :-)
                { "y", "0" }, // <- I doubt the server cares about the y position of where the user clicked on the image submit button :-)
                { "login", "login" },
            };

            // We authenticate first
            client.UploadValues("http://example.com/login", values);

            // Now we can download
            client.DownloadFile("http://example.com/abc.tgz", @"c:\abc.tgz");
        }
    }
}

顺便说一句,您的代码的问题是,当您将第一个请求发送到应该访问受保护资源的第二个请求时,您没有传递服务器发出的身份验证 cookie。你传递的只是一些内容类型,没有cookie等等。像 cookie 一样的服务器 :-)

【讨论】:

  • 非常感谢达林..!
猜你喜欢
  • 2017-12-19
  • 2021-09-15
  • 1970-01-01
  • 2014-08-30
  • 2018-11-29
  • 2013-12-03
  • 2012-07-30
  • 1970-01-01
  • 2011-08-12
相关资源
最近更新 更多