您的位置:首页 > 其它

解决和排查 "必须使用适当的属性和方法修改 User-Agent" 错误时遇到的一些坑

2019-03-12 22:33 816 查看

解决 必须使用适当的属性和方法修改 User-Agent 错误

问题描述:近在项目中有一个需求为需要在

Http
的Header里面添加一个User-Agent参数,当请求时。项目本身的目标框架是
.NET Standard 2.0
。于是,在项目源码中发现了,最终调用的请求是使用
HttpWebRequest
来进行最后的封装和发送的。

首先是用

HttpRequest
包装的,代码如下:

request.Headers["User-Agent"] = "Windows 10.0.2.4";

然后到最后发请求时,

foreach
这个Headers 的 Dictionary<string,string> 类型的,然后copy to
HttpWebRequest
的这个Header中

foreach (var header in request.Headers)
{
httpWebRequest.Headers.Add(header.Key, header.Value);
}

本地也进行了相关的 UT 和 FT,一切都很完美。然后发布了新版本。

版本发布后的第三天,接到了用户的一个反馈说:

在调用最新Nuget版本的包后,请求调用一直报错:必须使用适当的属性和方法修改 User-Agent

首先先让用户回退到上一个版本的包,然后询问了用户的目标框架,用户说是

.Net framework 4.6.1
,刚开始我以为是不是我项目中引用的某个包不支持该版本啊,沿着这个思路,果然有所发现。我发现在获取用户当前系统版本的时候使用了这个类System.Runtime.InteropServices.RuntimeInformation.OSDescription,然后我发现这个属性可能为空,是不是属性空导致这个错误的呢?抱着试一试的态度,继续往下。

去官方文档看了看,这个包最低支持的.net framework 框架为 4.7.1 。我想,那如果我在目标框架中也包含了.net framework 4.7.1 会不会就不报错了呢。

继续沿着这个思路,我去寻找了: 如何在csproj 文件中指定多个 targetframework ,于是也便有了这个提问,如何发布多个 TargetFramework 的nuget 包 。当我设置完了这个目标框架,测试时发现,怎么还是不行呢。我发现我可能走错路了,可能根本就不是这个地方出现问题了,是不是我问题定位的有问题。而且奇怪的是,

为什么如果我本地目标框架是 .netcoreapp2.0 或者 .net standard2.0 时就不会报错呢?好奇怪。

于是,我继续开始设置断点进行调试,最终发现了,每次走到下面这段代码时就会曝出上面那条错误:

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,这个也坑死我了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐