3

WebClient 类中的 Headers 集合有一个非常奇怪的问题。

这是我的例子:

WebClient client = new WebClient();
        client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0");
        client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        client.Headers.Add("Accept-Language", "pl,en-us;q=0.7,en;q=0.3");

        Console.WriteLine("Before request:");
        foreach (string key in client.Headers)
        {
            Console.WriteLine(key + ": " + client.Headers[key]);
        }

        client.DownloadString("http://www.google.com");

        Console.WriteLine();
        Console.WriteLine("After request:");

        foreach (string key in client.Headers)
        {
            Console.WriteLine(key + ": " + client.Headers[key]);
        }

        Console.ReadLine();

我运行这个简单程序的结果:

Before request:
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-us;q=0.7,en;q=0.3

After request:
Accept-Language: pl,en-us;q=0.7,en;q=0.3

为什么我的标题消失了?

4

1 回答 1

3

因为标头已发送并且 webclient 完成了它的工作。如果您希望下一个请求使用相同的标头,则应再次添加它们。

于 2013-07-29T11:21:51.147 回答