0

我正在使用基本身份验证连接到服务器,之后我使用以下代码在 Webview 中调用 URL:

WebView.Source(new Uri("https:(//UrlHere"));  

Webview 建立了一个登录窗口,但我已经登录了。为什么会这样?我怎样才能防止这种情况?

ia 对服务器进行身份验证的方式:

   private async void HttpClientCall(object sender, RoutedEventArgs e)
{

    System.Diagnostics.Debug.WriteLine(this.GetType().Name + ": HTTPCLientCall entered");

    //System.Diagnostics.Debug.WriteLine("NetworkConnectivityLevel.InternetAccess: " + NetworkConnectivityLevel.InternetAccess);

    //use this, for checking the network connectivity
    System.Diagnostics.Debug.WriteLine("GetIsNetworkAvailable: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable());

    //var msg = new Windows.UI.Popups.MessageDialog("GetIsNetworkAvailable: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable());
    //msg.ShowAsync();


    HttpClient httpClient = new HttpClient();


    // Assign the authentication headers
    httpClient.DefaultRequestHeaders.Authorization = CreateBasicHeader("username", "password");
    System.Diagnostics.Debug.WriteLine("httpClient.DefaultRequestHeaders.Authorization: " + httpClient.DefaultRequestHeaders.Authorization);


    // Call out to the site
    HttpResponseMessage response = await httpClient.GetAsync("https://URLHere");
    System.Diagnostics.Debug.WriteLine("response: " + response);
    string responseAsString = await response.Content.ReadAsStringAsync();
    System.Diagnostics.Debug.WriteLine("response string:" + responseAsString);

    //WebViewP.Source = new Uri("https://URLHere");
}




public AuthenticationHeaderValue CreateBasicHeader(string username, string password)
{
    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(username + ":" + password);

    String logindata = (username + ":" + password);
    System.Diagnostics.Debug.WriteLine("AuthenticationHeaderValue: " + new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)));

    return new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
}

那么我该如何解决呢?

4

1 回答 1

0

这是一个很长的镜头,但您可以尝试在 HttpClientHandler 上设置凭据并希望 WebView 能够接受它。

var handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(username,password);
HttpClient httpClient = new HttpClient(handler);

问题是 WebView 正在发出自己的独立请求,因此您使用 HttpClient 发出的任何请求都独立于 WebView 发出的请求

于 2013-10-29T20:03:33.477 回答