具有基本身份验证的HTTP请求始终返回401

时间:2016-11-16 19:05:31

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

我试图在UWP(Windows 10)应用中进行GET。我尝试了几种方法但总是返回401。

在邮差中,它运作良好,但我可以'似乎让它在我的应用程序中工作。我错过了什么。

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

方法1:

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

        }

方法2:

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

方法3:

element.all(by.exactRepeater("thread in threads"));

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

2 个答案:

答案 0 :(得分:1)

我先试试这个:

检查你的“MYAUTHTOKEN”,它通常是用户名:密码的组合,并且是64位编码。因此,如果您的用户名为“user”且密码为“pass”,则需要base64编码“user:pass”

var request = WebRequest.Create("https://api.fos.be/person/login.json");
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Text.Encoding.UTF8.GetBytes("user:pass"));
var response = await request.GetResponseAsync();

答案 1 :(得分:1)

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