c# - 如何更改 HttpWebRequest 中 UserAgent 的编码?

标签 c# .net ios unicode user-agent

今天我在尝试使用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.43rfc 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/

相关文章:

c# - Windows 窗体与 WPF

iphone - iOS 静态库和 Core Graphics 错误 : expected '=' , ',' , ';' , 'asm' 或 '__attribute__' 之前

c# - 如何编写使用 return 关键字的单元测试用例?

c# - 单击按钮时在表单上运行所有验证事件

javascript - 如何在网页中访问基本的 JSIL 库(来自 C#)?

c# - 我可以在 WPF 中使用 NotifyIcon 吗?

c# - 设计一个只有一些实现需要 IDisposable 的接口(interface)

c# - 如何创建默认 GlobalResourceProvider 的实例?

iphone - UIViewController 类中的 presentModalViewController

由于 NSInvalidUnarchiveOperationException 导致 iOS11 WKWebview 崩溃