c# - c# 永久设置 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 at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadString(Uri address)
如果我在第一个 DownloadString 之后设置用户代理,我不会得到异常。这是为什么?
解决方案
推荐阅读
- flutter - 如何在颤动中使用本地的.svg图标?
- flutter - 如何仅操作 ListView.builder 的一个元素
- socket.io - 如何让 socket.io 与 deno 一起工作?
- python - Python线程锁共享对象无法正常工作
- javascript - 不显示动态 div 中的图像
- python - python游标只返回第一列不为空的那些行
- linux - 如何更改某些文件的扩展名?
- ios - 使用 Accelerate 框架的对称带矩阵的特征值
- php - 从动态 php 文件生成 PDF
- microsoft-graph-api - 不支持endsWith过滤器?