Mak*_*luv 6 c# webview windows-store-apps win-universal-app uwp
我需要设置自定义UA并使用
httpRequestMessage.Headers.Add("User-Agent", "blahblah");
theWebView.NavigateWithHttpRequestMessage(httpRequestMessage);
Run Code Online (Sandbox Code Playgroud)
但是,如果我点击页面上的任何链接,这个UA会删除并设置默认UA.
我发现了同样的问题 WebView - 在每个请求上定义用户代理但是它可能在1607中修复了吗?
WebView不是通用浏览器,它确实有一些现在不支持的"限制".没有API可以设置每个请求中使用的默认User-Agent.作为一种解决方法,我们可以使用WebView.NavigationStarting事件以及WebView.NavigateWithHttpRequestMessage方法在每个请求中设置User-Agent.
有关如何执行此操作的详细信息,请参阅此答案.这里的关键点是删除NavigationStarting
事件处理程序并取消原始请求中的导航,然后添加处理程序NavigateWithHttpRequestMessage
以确保NavigationStarting
事件可以捕获下一个请求,如下所示:
WebView wb = new WebView();
wb.NavigationStarting += Wb_NavigationStarting;
...
private void NavigateWithHeader(Uri uri)
{
var requestMsg = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, uri);
requestMsg.Headers.Add("User-Agent", "blahblah");
wb.NavigateWithHttpRequestMessage(requestMsg);
wb.NavigationStarting += Wb_NavigationStarting;
}
private void Wb_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
wb.NavigationStarting -= Wb_NavigationStarting;
args.Cancel = true;
NavigateWithHeader(args.Uri);
}
Run Code Online (Sandbox Code Playgroud)
此外,欢迎您在UserVoice上投票以分享您的反馈.
归档时间: |
|
查看次数: |
1875 次 |
最近记录: |