代码之家  ›  专栏  ›  技术社区  ›  Srikanth Reddy

HttpClient Get call returning statuscode 401 unauthorized In C未经授权#

  •  0
  • Srikanth Reddy  · 技术社区  · 4 年前

    我正在尝试通过添加一些头来通过C调用HttpClient。通过抛出预期的带有200个状态码的空结果,请求工作正常。但同样的方法在C代码中不起作用。我的代码如下:

    public static Response TestAbcAPICall()
            {
                Response response = new Response();
    
                try
                {
                    var endpoint = "https:/abc.com/path/?script=435&deploy=1&type=vendor&id=9797";
    
                    
                    using (var client = new System.Net.Http.HttpClient())
                    {
                        client.BaseAddress = new Uri(endpoint);
                        //way - 1 Adding Authorization header
    
                        var authenticationHeaderValue = new AuthenticationHeaderValue("Authorization", "NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
                        client.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
    
                        // Adding contennt-type header
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                        var getTask = client.GetAsync(endpoint);
                        getTask.Wait();
                        var result = getTask.Result;
    
                        if (result.IsSuccessStatusCode)
                        {
                            var RESULTS = result.Content.ReadAsStringAsync().Result;
                            //response = JsonConvert.DeserializeObject<Response>(RESULTS);
                            if (response.flag)
                            {
                                response.flag = true;
                            }
                            else
                            {
                                response.flag = false;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.message = ex.Message;
                    response.flag = false;
                }
    
                return response;
            }
    

    具有200状态图像的参考弧工作: Working with ARC

    0 回复  |  直到 4 年前
        1
  •  0
  •   Alex K.    4 年前

    你的 AuthenticationHeaderValue 发送请求时,auth头将如下所示:

    Authorization: Authorization NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444
    

    正如你看到的复制品 Authorization 打破它。

    而是使用:

    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
    

    作为旁白,你真的只应该创造一个 HttpClient 实例并在应用程序的整个生命周期中重用它。

        2
  •  0
  •   Alen.Toma    4 年前

    你的授权应该如下所示

    var authenticationHeaderValue = new AuthenticationHeaderValue("NLAuth", "nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
                        client.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
    

    或者

    var authenticationHeaderValue = new AuthenticationHeaderValue("NLAuth nlauth_account=1249484_SKB1, nlauth_email=restletuser1@org.in, nlauth_signature=Breaths123!, nlauth_role=4444");
                        client.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
    

    已经有关于stackoverflow的帖子涉及这个问题 here