HttpClient授权问题 [英] Httpclient authorization issue

查看:171
本文介绍了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       

推荐答案

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


如果我理解的话正确地,您的API仅接受确切的字符串apwerfhafdh>0923817adfhhasfd<9作为授权标头. HttpClient拒绝带有FormatException的确切值的原因是因为它是无效的Authorization值:有效的Authorization值的格式为[type] [credentials],与您的X-ApiKey code格式一样.如果该API提供了Unauthorized,则表示该API仅接受严格无效的Authorization值,因此请检查该API是否确实不支持有效的 type 或告诉谁编写了该API来解决此问题,并确保有一些有效的Authorization标头被接受.
If I understand it correctly, your API only accepts the exact string apwerfhafdh>0923817adfhhasfd<9 as Authorization header. Why HttpClient rejects that exact value with a FormatException, is because it is an invalid Authorization value: valid Authorization values are of the format [type] [credentials], so like your X-ApiKey code format. If the API gives Unauthorized, it means that the API only accepts strictly invalid Authorization values, so either check if the API really supports no valid type or tell whoever wrote the API to fix this issue and make sure that there is some valid Authorization header that''s accepted.


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

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