1

I'm having a hard time trying to consume a REST service from Cielo (credit/debit card gateway company). If I use postman it works:

POST /1/sales/ HTTP/1.1
Host: apisandbox.cieloecommerce.cielo.com.br
MerchantKey: my_key
Content-Type: application/json
MerchantId: merc_id
Cache-Control: no-cache
Postman-Token: 6643cc5a-173a-f5db-8924-85ea8b7bbb55

{"MerchantId":"00000000-0000-0000-0000-000000000000","MerchantKey":null,"RequestId":"00000000-0000-0000-0000-000000000000","MerchantOrderId":"1223","Customer":{"Name":"Emerson Fitchy"},"Payment":{"PaymentId":"00000000-0000-0000-0000-000000000000","Type":"CreditCard","Amount":15700,"Installments":1,"Provider":null,"ProofOfSale":null,"Tid":null,"AuthorizationCode":null,"SoftDescriptor":null,"ECI":null,"Status":0,"ReturnCode":null,"ReturnMessage":null,"CreditCard":{"CardNumber":"0000000000000001","Holder":"Emerson Fitchy Santis","ExpirationDate":"12/2022","SecurityCode":"154","Brand":"Visa"}}}

And this is the C# code (at the moment, I tried also with RestSharp and HttpClient with the same results):

var webrequest = (HttpWebRequest)WebRequest.Create(Constants.Cielo.GetSalesUrl());

webrequest.ContentType = "application/json";
webrequest.Method = "POST";
webrequest.Headers.Add("MerchantId", Constants.Cielo.Sandbox.MerchantId.ToString());
webrequest.Headers.Add("MerchantKey", Constants.Cielo.Sandbox.MerchantKey);

using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
{
    var json = JsonConvert.SerializeObject(sale);
    streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)webrequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    var x = result;
}

Any ideas on what am I doing wrong? I tried using Fiddler 4 but it's not picking up this request (I don't know why) and WireShark picks it up, but doesn't show the information the way Fiddler does (Maybe it's because I'm using https?).

Any ideas?

Thanks!

EDIT

Response/Exception

enter image description here

eestein
  • 4,914
  • 8
  • 54
  • 93
  • It's possible the api doesn't like your user-agent or are missing required headers. See also - http://stackoverflow.com/a/15031536/2779990 – Stinky Towel Oct 12 '16 at 16:26
  • @StinkyTowel thank you for your comment. I'm gonna try to add the user-agent and get back to you. About the headers I doubt, since I'm adding the same ones you saw on postman. – eestein Oct 12 '16 at 16:39
  • @StinkyTowel unfortunately it wasn't the useragent... :/ `webrequest.UserAgent = "Mozilla/5.0 ..."; ` – eestein Oct 12 '16 at 16:44
  • Are you running PostMan and your httpclient with different accounts perhaps? The exception notes a remote host, but it could be a firewall in the way. – Stinky Towel Oct 12 '16 at 18:26
  • 1
    @StinkyTowel technically yes, PostMan I run on my mac and the code itself on my windows machine inside parallels. But I just installed postman on windows (and windows firewall is off ever since it was installed) and the same thing. It works on postman but not the code. :/ – eestein Oct 13 '16 at 11:49

2 Answers2

0

I just had the same issue where I suddenly was unable to make calls to a specific website from a Windows Server where I was running a console application on .NET 4.7.2. I was able to make the call in Postman from the same machine, but started getting the An existing connection was forcibly closed by the remote host.

It turns out that the sever had TLS 1.2 disabled. I was able to resolve it following the steps in this answer. I still am not sure if this setting was changed on the server or if the website started enforcing it, but it resolved the issue for me.

Matt N.
  • 197
  • 1
  • 1
  • 9
-1

Try this client and see if it you get a different result:

var client = new HttpClient();

using (HttpRequestMessage request = new HttpRequestMessage())
{
    request.Method = HttpMethod.Post;
    request.RequestUri = new Uri(Constants.Cielo.GetSalesUrl(), UriKind.Absolute);
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var requestContent = JsonConvert.SerializeObject(sale);
    request.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");

    request.Headers.Add("MerchantId", Constants.Cielo.Sandbox.MerchantId.ToString());
    request.Headers.Add("MerchantKey", Constants.Cielo.Sandbox.MerchantKey);

    using (HttpResponseMessage response = await client.SendAsync(request))
    {
        if (response.IsSuccessStatusCode)
        {
            if (response.Content != null)
            {
                var rawJson = await response.Content.ReadAsStringAsync();
                // do stuff - map to type, etc.
            }
        }

        return something;
    }
}
Stinky Towel
  • 768
  • 6
  • 26
  • Nothing... same thing... I'm gonna try to run a sample code on Xamarin Studio on my mac, just to make sure it's not some weird error on Windows. – eestein Oct 13 '16 at 15:55
  • Do you get a 200? What's the response/exception? – Stinky Towel Oct 13 '16 at 15:56
  • 2
    Have you determined if it's a TLS issue? Have you tried different settings on the security protocol? Since this is most likely PCI, you may need to ensure at least TLS 1.1 or 1.2 **ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls2** – Stinky Towel Oct 13 '16 at 17:07