Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make a HttpClient Call through C# by adding some headers. The request is working fine by throwing the empty result as expected with 200 status code. But the same is not working in C# code. My code is like below :

C#
<pre>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;
        }



What I have tried:

Tried with posting the headers as used in code with
AuthenticationHeaderValue
Posted
Updated 30-Nov-20 21:33pm
Comments
BabyYoda 30-Nov-20 15:18pm    
What exactly is the issue?
Tech Box To Unbox 30-Nov-20 23:41pm    
It's giving the response as 401 unauthorized. But working fine in ARC or postman

1 solution

Look at the AuthenticationHeaderValue constructor you're calling:
AuthenticationHeaderValue Constructor (System.Net.Http.Headers) | Microsoft Docs[^]

The first parameter is the authentication scheme. The second parameter is the scheme parameter.

You are passing a schema of "Authorization", giving a header of:
Authorization: Authorization NLAuth ...
I suspect you want the header to be:
Authorization: NLAuth ...
which means "NLAuth" is the scheme:
C#
var authenticationHeaderValue = new AuthenticationHeaderValue("NLAuth", "nlauth_account=...");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900