WebView的自定义用户代理

时间:2012-12-08 04:12:53

标签: windows-8 microsoft-metro

我可以为WebView设置自定义用户代理吗?

我需要展示网站的移动风格。

3 个答案:

答案 0 :(得分:4)

这很容易做到:

string ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X)" + "AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
httpRequestMessage.Headers.Add("User-Agent",ua);
webView1.NavigateWithHttpRequestMessage(hrm);

答案 1 :(得分:2)

根据这个MSDN Forum posting你不能。您是否可以托管轻量级代理服务(例如Azure网站)来代理请求?

答案 2 :(得分:2)

您可以使用自定义用户代理加载HTML,然后将html传递给WebView

加载HTML

var handler = new HttpClientHandler {AllowAutoRedirect = false};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("user-agent",
                                 "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; 
                                  WOW64; Trident/6.0)");
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
var html = await response.Content.ReadAsStringAsync();

将html分配给WebView

WebView.NavigateToString(html);