0

I am trying to integrate a payment platform to my web application but one of the requirements to query a transaction is to send a hashed value (of certain variables) as a header using a get request. This is what I have tried:

string hashcode = "3409877" + "117" + "D3D1D05AFE42AD508";
var hashedBytes = SHA512.Create().ComputeHash(Encoding.UTF8.GetBytes(hashcode));
// Get the hashed string.  
var hash = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
hash = hash.ToString().ToUpper();
var amount_kobo = Convert.ToInt32(model.TransactionAmount * 100);
string url = "https://sandbox.interswitchng.com/collections/api/v1/gettransaction.json?productid=117&transactionreference=" + model.TransactionReference + "&amount=" + amount_kobo;

using (var client = new HttpClient())
{

#if DNX451
    // ignore server certificate error
    //ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => { return true; };
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
#elif DNXCORE50
    // no implementation for the target DNX
#endif
    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Clear();


client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/html"));
    client.DefaultRequestHeaders.Add("Hash",hash);


    HttpResponseMessage response = await client.GetAsync(url);
    string responsestr = "";
    if (response.IsSuccessStatusCode)
    {
        responsestr = await response.Content.ReadAsStringAsync();
        return Json(new { success = true, response = responsestr, hash = hash });
    }
}

but my hash is not sent, when I inspect the browser after sending the request I do not see my hash header, also am supposed to send the header as a hash type but I have no idea what to do.

I have tried changing this line to:

 client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("hash"));

still it doesn't work.

Update Here is a screenshot of the request headers in the browser Request headers

Seems my header is not sent at all.

Just Shadow
  • 10,860
  • 6
  • 57
  • 75

2 Answers2

1

Try to use client.DefaultRequestHeaders.Add("Hash", hash) instead.

Your code tries to add an Accept request-header, which is used to specify what media types are acceptable for the response.

  • Thats not working, i updated my answer with the request headers shown in the browser and my hash header is not there...what could be stopping my header from being sent pls? – Chidiebere Onwunyirigbo Feb 08 '17 at 12:10
  • Thanks a lot, removed the client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("hash")); line and it worked. – Chidiebere Onwunyirigbo Feb 08 '17 at 12:45
0

After converting hash to string, your string may contain characters which are restricted for sending in GET requests. So you should consider to encode the string by using Uri.EscapeDataString() function before sending:

hash = Uri.EscapeDataString(hash);

Or convert hashedBytes to Base64 and encode just few symbols after it

Community
  • 1
  • 1
Just Shadow
  • 10,860
  • 6
  • 57
  • 75