5
I’m making a system to authenticate the Token (derived from the credit card) next to Cielo.
This token has special characters like +
Cielo receives the data via XML. I made the following code to send:
private String sendHttpRequest(String message)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (Stream stream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes("mensagem=" + message);
stream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result;
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
return result.ToString();
}
However the token arrives in a different way in Cielo returning me Token not found, being that it generated the Token. When contacting the support of why is their response:
The error occurs when sending the token request without converting URL-encoding, i.e., the special characters (for example +) is interpreted as spaces by the platform. This is because the platform receives XML in an HTTP request parameter, using the content-type applicationx-www-form-urlencoded. Technologies such as Java (Apache Httpcomponenthttpclient) perform this conversion automatically, becoming transparent to the developer. The establishment should evaluate and adjust the encoding (Urlencode) in its solution, otherwise other special characters will display the same problem.
I believe my code is correct when reading that url I tried to do something with Httputility.Urlencode(message); ex:
var t = HttpUtility.UrlEncode(message);
byte[] bytes = encoding.GetBytes ("mensagem=" + t);
But same mistake, before I call there and complain to Cielo I would like some help, because I may be making some mistake.
Is there a way I copy the Bytes and try to simulate this post by some browser plugin? like I do with Chrome’s 'Simple REST Client' plugin?
Can I debug and visualize something that I can help?
NOTE: When the token is without these special characters, the same code above works perfectly.
I will test, with the 2nd option of invalid request.. still testing..
– Dorathoto
@Dorathoto, strange, in any case I put an additional detail in the answer, I believe it has replaced the wrong excerpt.
– Tobias Mesquita
but I am now testing with Microsoft ASP.NET Web API 2.2 Client seems to be a better solution for what I saw...how to force it into utf-8 ?
– Dorathoto
Error: {"La codifica Nella dichiarazione 'ISO-8859-1' non corrisponde alla codifica del documento 'utf-8'." } My windows is Italian, but da para entender...the error...pq will be?
– Dorathoto
@Dorathoto, as you can see in the source of
FormUrlEncodedContent
, the same makes use of theHttpRuleParser.DefaultHttpEncoding
, which in turn receives the Encounteriso-8859-1
... opitionally you can replace thenew FormUrlEncodedContent(data)
fornew StringContent( HttpUtility.UrlEncode("mensagem=" + message), Encoding.UTF8, "application/x-www-form-urlencoded")
– Tobias Mesquita
if you replace the
FormUrlEncodedContent
forStringContent
, will not need to make modifications toHeaders
ofconteudo
... you can also useUri.EscapeDataString("mensagem=" + message).Replace("%20", "+")
instead ofHttpUtility.UrlEncode("mensagem=" + message)
– Tobias Mesquita
Let’s go continue this discussion in chat.
– Dorathoto
you could kindly include in your reply the end to receive the return after posting, see in my question I just add the end, and I’m not able to use your answer. if possible of course
– Dorathoto