的WebView - 每个请求定义的User-Agent [英] WebView - Define User-Agent on every request
问题描述
目前,我设置的请求消息的User-Agent如下:
Currently, I am setting the User-Agent of a request message as follows:
var rm = new HttpRequestMessage(HttpMethod.Post, new Uri("http://example.com"));
rm.Headers.Add("User-Agent", "UserAgentString");
webView.NavigateWithHttpRequestMessage(rm);
一切正常。
,当我导航到另一个页面时,通过单击网站上的链接,例如,用户代理重置为 WebView
的默认值。
But, when I am navigating to an another page, by clicking a link on the site for example, the User-Agent resets to the default of the WebView
.
是否有任何方法可以永久设置用户代理或在每个请求时更改用户代理?
Is there any way of setting the User-Agent permanently or changing it on every request made?
感谢您,
乔治
Thanks, George
推荐答案
NavigationStarting
发生在webview导航到新内容之前。您可以取消该操作,获取 args.Uri
,然后使用 HttpRequestMessage
进行导航。还有 FrameNavigationStarting
。
NavigationStarting
occurs before the webview navigates to new content. You can cancel that operation get the args.Uri
and navigate with HttpRequestMessage
. There is also FrameNavigationStarting
.
WebView wb = new WebView();
wb.NavigationStarting += A_NavigationStarting;
private void A_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
//this will throw `StackOverflowException` so you have to add a condition here
if(handled)
{
args.Cancel = true;
var rm = new HttpRequestMessage(HttpMethod.Post, args.Uri);
rm.Headers.Add("User-Agent", "UserAgentString");
sender.NavigateWithHttpRequestMessage(rm);
}
}
这篇关于的WebView - 每个请求定义的User-Agent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!