C#:web客户端下载html页面的源代码,而不是实际的资源

本文关键字:资源 源代码 客户端 web 下载 html | 更新日期: 2023-09-27 17:58:57

我采用了MSDN博客中的这段代码,并添加了Web客户端来下载资源。。

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"页面的源文件。。我哪里错了?

Web客户端是否有任何属性可以用来查看错误。。即。。基址等?

C#:web客户端下载html页面的源代码,而不是实际的资源

让我们简化一下,好吗:

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的服务器:-)