I'm trying to consume a WebService via WebRequest in C # and simply can not get any response from the server. However, via Postman I get the answer using the same parameters.
code:
var JsonCliente = JsonConvert.SerializeObject(clienteMP);
var payload = UTF8Encoding.UTF8.GetBytes(JsonCliente);
String username = APIKey;
String password = "";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
string URLRequest = URI;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLRequest);
request.Headers.Add("Authorization", "Basic " + encoded);
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
var settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
var data = JsonConvert.SerializeObject(clienteMP, Newtonsoft.Json.Formatting.None, settings);
writer.Write(data);
writer.Flush();
writer.Close();
}
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
dynamic myObject = JsonConvert.DeserializeObject<dynamic>(reader.ReadToEnd());
return myObject.ToString();
}
the exception is given in line WebResponse response = request.GetResponse();
, the error message is:
InnerException = {"Termination of existing connection by remote host was forced"}
Message="Unable to read transport connection data: An existing connection was forced by the remote host."