我連接到服務器使用基本身份驗證,之後我打電話網頁視圖的URL與下面的代碼後,再次顯示Loginscreen -Window,但我已經登錄。爲什麼會發生這種情況? 我該如何預防?的WebView通過基本身份驗證的登錄與HttpClient的
我的方式對服務器的身份驗證:
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));
}
那麼我怎樣才能解決?
有可能是服務器設置一些身份驗證Cookie做出的。使用fiddler並在服務器響應中查找set-cookies標頭。如果是這樣,那麼你可以清除cookie容器,以防止被「登錄」。 –
我不想阻止被「登錄」。我想保持登錄狀態。當httpClient完成授權後,我想導航/顯示頁面。如果服務器設置了一個cookie,我該怎麼做?保存該cookie,然後再次調用該頁面?還是有另一種方式來授權時調用webView? – brush51