今天我在尝试使用HttpWebRequest 发送请求时遇到了UserAgent 编码的问题。
通常,UserAgent 由拉丁字母和标点符号组成。但是,我需要模拟一个 iOS 应用程序的 Web 请求,该应用程序的 UA 包含一些 Unicode(特别是中文)字符。
使用 Fiddler 获取原始请求,我发现应用程序在其 UA 中使用了 Unicode 编码。我无法在 C# 中发布它;我收到此错误:
You may not try to send Unicode in UserAgent. But it is really important for my project. Now I can simulate the request of App without the bytes of UA.
如何更改 UA 编码?
最佳答案
根据标准(rfc 2616 (http/1.1), sec 2.2, 3.8. 14.43 和 rfc 2047 (mime, part 3), sec 4, 5),除了 iso-8859-1 之外,您不能对任何 http header 字段(如用户代理)使用任何其他编码。
但是,您可以应用 rfc 2047 的编码方案将 unicode 字符串映射到 8859-1 - 字符串;简而言之,您用字符集标识符包装文本,并用各自编码中八位位组序列的十六进制值替换 unicode 代码点。
例子:
User-Agent: Million-€-Browser
变为 User-Agent: =?utf-8?q?Million-=e2=82=ac-Browser?=
,其中 e2 82 ac
是欧元符号的 utf-8 八位位组序列。
关于c# - 如何更改 HttpWebRequest 中 UserAgent 的编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12227801/