本文介绍了Web客户端头类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用与cookies WebClient类这里提到:http://stackoverflow.com/questions/1777221/c-using-cookiecontainer-with-webclient-class

I am using the WebClient class with cookies as mentioned here: http://stackoverflow.com/questions/1777221/c-using-cookiecontainer-with-webclient-class

都需要哪些步骤自定义用户代理添加到该Web客户端尽一切的要求吗?

What steps are required to add a custom user agent to every request made by this WebClient?

我试图把

Headers.Add(HttpRequestHeader.UserAgent, "...") 

行到

protected override WebRequest GetWebRequest

但没有奏效:这头必须使用适当的属​​性进行修改

but that did not work: "This header must be modified using the appropriate property".

推荐答案

从的,

using System;
using System.Net;
using System.IO;

    public class Test
    {
        public static void Main (string[] args)
        {
            if (args == null || args.Length == 0)
            {
                throw new ApplicationException ("Specify the URI of the resource to retrieve.");
            }
            WebClient client = new WebClient ();

            // Add a user agent header in case the 
            // requested URI contains a query.

            client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            Stream data = client.OpenRead (args[0]);
            StreamReader reader = new StreamReader (data);
            string s = reader.ReadToEnd ();
            Console.WriteLine (s);
            data.Close ();
            reader.Close ();
        }
    }

这篇关于Web客户端头类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:08