我正在尝试在UWP(Windows 10)应用程序中进行GET。我尝试了几种方法,但所有方法总是返回401。

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

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

方法1:

        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://,但必须将其设置为http s ://。解决方案是使用带有该线程中代码的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/

10-11 10:51