c# - 使用基本身份验证的 HTTP 请求始终返回 401

标签 c# windows uwp windows-10 windows-10-universal

我正在尝试在 UWP (Windows 10) 应用中执行 GET。我尝试了多种方法,但总是返回 401。

在 Postman 中它工作正常,但我似乎无法让它在我的应用程序中工作。我缺少什么。

这些是我尝试过的方法(全部返回401):

方法一:

        var request = WebRequest.Create("http://api.fos.be/person/login.json?login=200100593&password=pass");
        request.Headers["Authorization"] = "Basic MYAUTHTOKEN";
        var response = await request.GetResponseAsync();

方法2:

        const string uri = "http://api.fos.be/person/login.json?login=200100593&password=pass";
        var httpClientHandler = new HttpClientHandler();
        httpClientHandler.Credentials = new System.Net.NetworkCredential("MYUSERNAME", "MYPASSWORD");
        using (var client = new HttpClient(httpClientHandler))
        {
            var result = await client.GetAsync(uri);
            Debug.WriteLine(result.Content);

        }

方法3:

        var client = new RestClient("http://api.fos.be/person/login.json?login=200100593&password=pass");
        var request = new RestRequest(Method.GET);
        request.AddHeader("postman-token", "e2f84b21-05ed-2700-799e-295f5470c918");
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("authorization", "Basic MYAUTHTOKEN");
        IRestResponse response = await client.Execute(request);
        Debug.WriteLine(response.Content);

第三种方法是直接从 Postman 生成的代码,那么为什么它在那里工作而不是在我的应用程序中工作?

最佳答案

This线程帮助我找到了解决方案。我使用的是 http://,但我必须将其设为 https://。使用该线程中的代码的 HTTPS 就是解决方案。

这是我的最终代码:

    public static async void GetPerson()
    {
        //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("MYUSERNAME", "MYPASS");
        System.Diagnostics.Debug.WriteLine("httpClient.DefaultRequestHeaders.Authorization: " + httpClient.DefaultRequestHeaders.Authorization);
        // Call out to the site
        HttpResponseMessage response = await httpClient.GetAsync("https://api.fos.be/person/login.json?login=usern&password=pass");
        System.Diagnostics.Debug.WriteLine("response: " + response);
        string responseAsString = await response.Content.ReadAsStringAsync();
        System.Diagnostics.Debug.WriteLine("response string:" + responseAsString);
    }
    public static 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));
    }

关于c# - 使用基本身份验证的 HTTP 请求始终返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40640282/

相关文章:

c# - 键入时将文本设置为全部大写

c# - System.Collections 与 System.Collections.ObjectModel

c# - 如果 Windows 更新强制重启,如何重启我的应用程序?

c# - 无法从 UWP 打包项目 (UWP + Win32) 构建 ARM64

c# - 当我按下 Alt 键时,如何防止控制框获得焦点?

c# - 无法弄清楚计算时间表以在日历上显示日期的例程 - 算法

windows - 批处理 : set a variable with a filename

python - 如何在 Windows 上打开包含选定项目的文件夹

UWP 应用与 Windows 服务的通信

c# - 为什么我无法在 WebView (UWP) 中打开外部浏览器中的链接?