1

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 :

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

Reference ARC worrking with 200 status Image : Working with ARC

Srikanth Reddy
  • 173
  • 3
  • 14
  • client.BaseAddress Is not needed to be set, you already have a full address. – Alen.Toma Nov 30 '20 at 16:39
  • @Alen.Toma Yes of course Thank you, Commented it. still facing the same.. – Srikanth Reddy Nov 30 '20 at 16:42
  • The working code is using HTTPS (secure). Are you getting any response with c#? If you are not getting a response than the TLS authentication is failing which occurs before a request is sent. The normal solution is to specify the TLS version : ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; – jdweng Nov 30 '20 at 16:45
  • I think hes gettings response, Try and see what is the response Eg `result.StatusCode == HttpStatusCode.Unauthorized` And also this is Get request so you dont need to set `application/json` you are aready reading it as a string – Alen.Toma Nov 30 '20 at 16:50
  • Yes, Within "getTask" I'm getting the "result.IsSuccessStatusCode" as "false" – Srikanth Reddy Nov 30 '20 at 16:51
  • @Alen.Toma , The result of "result.StatusCode == HttpStatusCode.Unauthorized" is true. – Srikanth Reddy Nov 30 '20 at 16:57
  • Then you are doing somthing wrong with your Authorization. and there is the problem. – Alen.Toma Nov 30 '20 at 16:59
  • What would be wrong. Because the things I'm passing to ARC or postman passing the same things as headers. – Srikanth Reddy Nov 30 '20 at 17:02

2 Answers2

0

Your AuthenticationHeaderValue auth header will look like this when the request is sent:

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

As you can see the duplicate Authorization breaks it.

Instead use:

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

As an aside you are really only supposed to create a single HttpClient instance and reuse it throughout the lifetime of your app.

Alex K.
  • 159,548
  • 29
  • 245
  • 267
0

Your Auth should look like below

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

Or

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

There is already post on stackoverflow that cover this issue here

Alen.Toma
  • 4,792
  • 2
  • 11
  • 24
  • Toma , The first one made to add header but giving 401 unauthorized response. The second one not even accepting as header, Instead throwing exception like format is invalid. – Srikanth Reddy Dec 01 '20 at 05:31