如何在webview中设置用户代理?

时间:2019-03-26 08:27:54

标签: c# webview set agent

我正在尝试在Webview中设置用户代理

webViewIcloud.NavigationStarting += webViewIcloud_NavigationStarting;
            webViewIcloud.Navigate(new Uri("https://www.icloud.com/#iclouddrive"));

private void NavigateWithHeader(Uri uri)
        {
            var requestMsg = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, uri);
            requestMsg.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.699.0 Safari/534.24.");
            webViewIcloud.NavigateWithHttpRequestMessage(requestMsg);
            webViewIcloud.NavigationStarting += webViewIcloud_NavigationStarting;
        }

        private void webViewIcloud_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
        {
            webViewIcloud.NavigationStarting -= webViewIcloud_NavigationStarting;
            args.Cancel = true;
            NavigateWithHeader(args.Uri);
        }

0 个答案:

没有答案