How to use basic http authentication on Windows rt- Visual Studio 2012 - C#

扶醉桌前 提交于 2019-12-25 01:46:59

问题


how can i use basic http Authentication for a HTTPS URL Windows 8 Store App. I am using Visual Studio 2012, C# and XAML.

Is there any point to keep attention when i use an HTTPS URL?

i have tried these following methods:

#1: ###Code is UPDATED - Final running solution

        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));
    }

i just want to display a HTTPS website where authentication is needed. i am getting a Warning from visual studio and it stop working: An error occurred while sending the request.

´A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll´


回答1:


You have a more simple way to perform basic authentication.

  • First create an HttpClientHandler instance.
  • You can set Credentials property of your HttpClientHandler instance according to your needs.
  • Initialize your HttpClient using constructor that accepts an HttpMessageHandler instance.


来源:https://stackoverflow.com/questions/19497662/how-to-use-basic-http-authentication-on-windows-rt-visual-studio-2012-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!