如何在Windows rt-Visual Studio 2012上使用基本的HTTP身份验证-C# [英] How to use basic http authentication on Windows rt- Visual Studio 2012 - C#

查看:142
本文介绍了如何在Windows rt-Visual Studio 2012上使用基本的HTTP身份验证-C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何对HTTPS URL Windows 8 Store App使用基本http身份验证. 我正在使用Visual Studio 2012,C#和XAML.

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

当我使用HTTPS URL时,有什么需要注意的地方吗?

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

我尝试了以下方法:

#1 : ###代码已更新-最终运行的解决方案

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

我只想显示一个需要身份验证的HTTPS网站. 我从Visual Studio得到警告,但它停止工作: 发送请求时出错.

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.

mscorlib.dll中发生了类型为'System.Net.Http.HttpRequestException'的第一次机会例外.

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

推荐答案

您有一种更简单的方法来执行基本身份验证.

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.

这篇关于如何在Windows rt-Visual Studio 2012上使用基本的HTTP身份验证-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆