问题描述:近在项目中有一个需求为须要在 Http
的Header里面添加一个User-Agent参数,当请求时。项目自己的目标框架是 .NET Standard 2.0
。因而,在项目源码中发现了,最终调用的请求是使用 HttpWebRequest
来进行最后的封装和发送的。web
首先是用 HttpRequest
包装的,代码以下:c#
request.Headers["User-Agent"] = "Windows 10.0.2.4";
而后到最后发请求时,foreach
这个Headers 的 Dictionary<string,string> 类型的,而后copy to HttpWebRequest
的这个Header中api
foreach (var header in request.Headers) { httpWebRequest.Headers.Add(header.Key, header.Value); }
本地也进行了相关的 UT 和 FT,一切都很完美。而后发布了新版本。app
版本发布后的第三天,接到了用户的一个反馈说:框架
在调用最新Nuget版本的包后,请求调用一直报错:必须使用适当的属性和方法修改 User-Agent测试
首先先让用户回退到上一个版本的包,而后询问了用户的目标框架,用户说是.Net framework 4.6.1
,刚开始我觉得是否是我项目中引用的某个包不支持该版本啊,沿着这个思路,果真有所发现。我发如今获取用户当前系统版本的时候使用了这个类System.Runtime.InteropServices.RuntimeInformation.OSDescription,而后我发现这个属性可能为空,是否是属性空致使这个错误的呢?抱着试一试的态度,继续往下。.net
去官方文档看了看,这个包最低支持的.net framework 框架为 4.7.1 。我想,那若是我在目标框架中也包含了.net framework 4.7.1 会不会就不报错了呢。调试
继续沿着这个思路,我去寻找了: 如何在csproj 文件中指定多个 targetframework ,因而也便有了这个提问,如何发布多个 TargetFramework 的nuget 包 。当我设置完了这个目标框架,测试时发现,怎么仍是不行呢。我发现我可能走错路了,可能根本就不是这个地方出现问题了,是否是我问题定位的有问题。并且奇怪的是,code
为何若是我本地目标框架是 .netcoreapp2.0 或者 .net standard2.0 时就不会报错呢?好奇怪。orm
因而,我继续开始设置断点进行调试,最终发现了,每次走到下面这段代码时就会曝出上面那条错误:
httpWebRequest.Headers.Add(header.Key, header.Value);
这究竟是为何呢,为何添加其余属性时就不会有这个错,终于仍是在微软的官方文档上找到了答案:
HttpWebRequest exposes common HTTP header values sent to the Internet resource as properties, set by methods, or set by the system; the following table contains a complete list. You can set other headers in the Headers property as name/value pairs. Note that servers and caches may change or add headers during the request.
The following table lists the HTTP headers that are set either by properties or methods or the system.
Header Set by Accept Set by the Accept property. Connection Set by the Connection property, KeepAlive property. Content-Length Set by the ContentLength property. Content-Type Set by the ContentType property. Expect Set by the Expect property. Date Set by the system to current date. Host Set by the system to current host information. If-Modified-Since Set by the IfModifiedSince property. Range Set by the AddRange method. Referer Set by the Referer property. Transfer-Encoding Set by the TransferEncoding property (the SendChunked property must be true
).User-Agent Set by the UserAgent property.
也就是说呢,若是你的Headers中没有包含上述的12个属性,我就当成它为保留属性把,你均可以用如下命令设置
httpWebRequest.Headers.Add(header.Key, header.Value);
然而当涉及到上述的属性的话,你就须要像下面这样
if(header.key.contains("User-Agent")) { httpWebRequest.UserAgent = "Set as you like"; }
总结起来仍是经验不够,小问题定位错误致使排查错了方向,一旦找对方向,就很容易解决啦。
PS:下一篇写一下 httpWebrequest 中的timeout,这个也坑死我了。