我可以为 a 设置自定义用户代理WebView
吗?
我需要展示网站的移动风格。
这很容易做到:
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);
根据此MSDN 论坛帖子,您不能。您能否托管一个轻量级代理服务(例如 Azure 网站)来为您代理请求?
您可以使用自定义用户代理加载 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);