Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 请求后WebClient中的标题消失_C#_Header_Webclient - Fatal编程技术网

C# 请求后WebClient中的标题消失

C# 请求后WebClient中的标题消失,c#,header,webclient,C#,Header,Webclient,我对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,appli

我对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

为什么我的标题会消失?

因为发送了标题,webclient完成了它的工作。如果您想为下一个请求添加相同的头,您应该再次添加它们