C# HttpClient 的自定义用户代理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13772937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom User Agent for HttpClient?
提问by MBZ
can I set a custom User Agent for a HttpClient?
我可以为 a 设置自定义用户代理HttpClient吗?
I need to view websites in their mobile form.
我需要以移动形式查看网站。
采纳答案by Mayank
Here you go
干得好
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2;
WOW64; Trident/6.0)");
回答by chue x
How about
怎么样
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "my-user-agent-name");
回答by DamienG
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd("MyAgent/1.0");
There is also a TryParseAdd if you want to catch bad attempts at adding it and Add if you want to create the ProductInfoHeaderValue with a name and version number yourself.
还有一个 TryParseAdd 如果您想捕获添加它的错误尝试和 Add 如果您想自己创建具有名称和版本号的 ProductInfoHeaderValue。

