c#永久设置WebClient用户代理

时间:2018-09-05 19:01:02

标签: c# .net webclient

我正在使用WebClient向github api发送一些请求。

var wc = new WebClient();
wc.Headers.Add("user-agent", "notempty"); 
wc.Headers.Add("authorization", "basic " + Convert.ToBase64String("user:password"));
var response = wc.DownloadString(url + "/labels");
...
var response = wc.DownloadString(url + "/issues");

首先我查询所有标签,然后查询所有问题

但是在第二个DownloadString上,出现以下异常:

  

未处理的异常:System.Net.WebException:服务器提交了协议冲突。 Section = ResponseStatusLine      在System.Net.WebClient.DownloadDataInternal处(URI地址,WebRequest和请求)      在System.Net.WebClient.DownloadString(URI地址)

如果我在第一个DownloadString之后设置了useragent,则不会出现异常。为什么会这样?

0 个答案:

没有答案