HttpClient授权问题

HttpClient授权问题

本文介绍了HttpClient授权问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个外部应用程序,该应用程序具有可从内部软件或其他应用程序中使用的Web API/服务.

我只是似乎无法使HttpClient正常工作的任何人.它可以与较旧的WebClient或RestClient等工具一起使用.我只能使其与HttpClient一起使用,这确实使我感到烦恼,因为我不知道为什么. IP和API密钥是伪造的.

因此,借助RestClient和其他技术,它可以工作:

We have an external app that has an Web API/Service to use from our internal software or different apps.

Anywho I just can''t seem to make the HttpClient work. It works with the older WebClient, or with RestClient among other tools.I just can''y make it work with HttpClient and it really bugs me, as in I don;t know why. The IP and API key are fake.

So with RestClient and other techonlogies it works:

public async Task<List<Employee>> LoadEmployees()
        {
            var client = new estClient("http://99.99.99.99:8080/api/v2/employee/");
            var request = new RestRequest(Method.GET);
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("accept-encoding", "gzip, deflate");
            request.AddHeader("Host", "99.99.99.99:8080");
            request.AddHeader("Cache-Control", "no-cache");
            request.AddHeader("Accept", "*/*");
            request.AddHeader("Authorization", "apwerfhafdh>0923817adfhhasfd<9");
            request.AddHeader("Content-Type", "application/json");
            IRestResponse responseData =  await client.ExecuteGetTaskAsync(request);

            if (responseData.StatusCode == System.Net.HttpStatusCode.OK)
            {
                List<Employee> emps = JsonConvert.DeserializeObject<List<Employee>>(responseData.Content);

                return emps;
            }
            else
            {
                throw new Exception($"{responseData.StatusCode} : {responseData.StatusDescription}");
            }
        }



但是使用HttpClient时,我无法设法使授权"部分正常工作.

我尝试过的事情:



but with HttpClient I can''t manage to make the Authorization part work.

What I have tried:

Client= new HttpClient();
            Client.DefaultRequestHeaders.Accept.Clear();
            Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            Client.DefaultRequestHeaders.Add("Accept", "*/*");
            Client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate");

            Client.DefaultRequestHeaders.Add("Authorization", "X-ApiKey apwerfhafdh>0923817adfhhasfd<9"); // I get Unathorized
//Client.DefaultRequestHeaders.Add("Authorization", "apwerfhafdh>0923817adfhhasfd<9"); // I get System.FormatException

            //CLient.DefaultRequestHeaders.Add("X-ApiKey", "apwerfhafdh>0923817adfhhasfd<9");   I get Unauthorized  
//Client       

推荐答案



但是使用HttpClient时,我无法设法使授权"部分正常工作.

我尝试过的事情:



but with HttpClient I can''t manage to make the Authorization part work.

What I have tried:

Client= new HttpClient();
            Client.DefaultRequestHeaders.Accept.Clear();
            Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            Client.DefaultRequestHeaders.Add("Accept", "*/*");
            Client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate");

            Client.DefaultRequestHeaders.Add("Authorization", "X-ApiKey apwerfhafdh>0923817adfhhasfd<9"); // I get Unathorized
//Client.DefaultRequestHeaders.Add("Authorization", "apwerfhafdh>0923817adfhhasfd<9"); // I get System.FormatException

            //CLient.DefaultRequestHeaders.Add("X-ApiKey", "apwerfhafdh>0923817adfhhasfd<9");   I get Unauthorized  
//Client       



这篇关于HttpClient授权问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:22