简体   繁体   English

C#-WP8.1应用程序中的Web用户代理

[英]C# - User Agent for Web in WP8.1 Apps

One question that's been confusing me and could really do with some insight. 一个让我感到困惑的问题,确实可以通过一些洞察力解决。

I need to retreive Json objects from a http service. 我需要从http服务检索Json对象。 When I tested this in a Console Window, I kept receiving a "Internal Server Error : 500" until I set the UserAgent property for the WebClient object. 当我在控制台窗口中对此进行测试时,我一直收到“内部服务器错误:500”,直到为WebClient对象设置了UserAgent属性。 Example: 例:

WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36");
content = client.DownloadString(url);

Now, if I need to do the same for a WP8.1 app, how would I detect (if I need to in the first place?) the UserAgent (and set it) and be able to retrieve the data? 现在,如果我需要对WP8.1应用程序执行相同的操作,该如何检测(如果首先需要?)UserAgent(并进行设置)并能够检索数据?

Thank you all. 谢谢你们。

Windows Phone 8.1 App will use HttpClient. Windows Phone 8.1 App将使用HttpClient。 By default there will not be a user agent set. 默认情况下,不会设置用户代理。 The default user-agent for the phones web browser is: 电话网络浏览器的默认用户代理为:

"Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 520) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537" “ Mozilla / 5.0(移动; Windows Phone 8.1; Android 4.0; ARM; Trident / 7.0;触摸; rv:11.0; IEMobile / 11.0;诺基亚; Lumia 520),例如iPhone OS 7_0_3 Mac OS X AppleWebKit / 537(KHTML,例如Gecko) )移动Safari / 537“

You can manually set the user-agent on the HttpRequestMessage.Headers.UserAgent property. 您可以在HttpRequestMessage.Headers.UserAgent属性上手动设置用户代理。

References: 参考文献:

HttpClient HttpClient的

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.web.http.headers.httprequestheadercollection.aspx https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.web.http.headers.httprequestheadercollection.aspx

User-Agent 用户代理

https://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx#ie11 \\ https://msdn.microsoft.com/zh-cn/library/ie/hh869301(v=vs.85).aspx#ie11 \\

The class libraries for using http do not add any User Agents by default. 默认情况下,使用http的类库不会添加任何用户代理。 See these lines from the msdn page: 请从msdn页面查看以下行:

By default, no user-agent header is sent with the HTTP request to the web service by the HttpClient object. 默认情况下,HttpClient对象不会将任何用户代理标头与HTTP请求一起发送到Web服务。 Some HTTP servers, including some Microsoft web servers, require that a user-agent header be included with the HTTP request sent from the client. 某些HTTP服务器(包括某些Microsoft Web服务器)要求从客户端发送的HTTP请求中包含用户代理标头。 The user-agent header is used by the HTTP server to determine how to format some HTTP pages so they render better on the client for different web browsers and form factors (mobile phones, for example). HTTP服务器使用用户代理标头来确定如何格式化某些HTTP页面,以便它们在客户端上针对不同的Web浏览器和形状因子(例如,移动电话)更好地呈现。 Some HTTP servers return an error if no user-agent header is present on the client request. 如果客户端请求上不存在用户代理标头,则某些HTTP服务器将返回错误。 We need to add a user-agent header to avoid these errors using classes in the Windows.Web.Http.Headers namespace. 我们需要使用Windows.Web.Http.Headers命名空间中的类来添加用户代理标头,以避免这些错误。 We add this header to the HttpClient.DefaultRequestHeaders property. 我们将此标头添加到HttpClient.DefaultRequestHeaders属性。

For more details, refer the link below: 有关更多详细信息,请参见下面的链接:

How to connect to an HTTP server using Windows.Web.Http.HttpClient (XAML) 如何使用Windows.Web.Http.HttpClient(XAML)连接到HTTP服务器

Also look at the answer below (by Bret Bentzinger) for the exact user agent string. 另外,请查看下面的答案(由Bret Bentzinger提供),以获取确切的用户代理字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM