I am developing an application in C # that connects to a web service but when I try to send the data via POST I get an error in my application in C #:
System.Net.Http.HttpRequestException: Error sending the request. --- > System.Net.WebException: Connection is terminated: Unexpected sending error. --- > System.IO.IOException: Can not write data of the transport connection: The interruption of the an existing connection by the remote host. --- > System.Net.Sockets.SocketException: The interruption has been forced an existing connection by the remote host
But there is something even more important when I send the data via Postman in RAW the data is sent correctly.
Here I send the data to the service
//crear la Instacia del Api
ApiServices api = new ApiServices();
//Envia las Modalidades
var Modali = manager.Getdata("Select * , descrip as Modalidad from modalidades with(nolock) ");
if (Modali.Rows.Count > 0)
{
//Inserta los Clientes en el Servidor
var data = JsonConvert.SerializeObject(Modali);
var data1 = JsonConvert.DeserializeObject<List<Modalidades>>(data);
var serial = JsonConvert.SerializeObject(data1);
await api.Post(serial, "", "Cuentasporcobrar/EnviarModalidades/590025");
EscribirLinea("Insertó los Datos de la modalidades en el Servidor");
}
this is the code that sends the POST request
public async Task<bool> Post(string Data, string token, string Controler)
{
try
{
var Cliente = new HttpClient
{
BaseAddress = new Uri(configuracion.ServicioRest )
};
Cliente.DefaultRequestHeaders.Accept.Clear();
Cliente.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
StringContent content = new StringContent(Data, Encoding.UTF8, "application/json");
var Url = string.Format("{0}", Controler);
var Respuesta = await Cliente.PostAsync(Url, content);
if (!Respuesta.IsSuccessStatusCode)
{
return false;
}
return true;
}
catch (Exception)
{
}
return false;
}
NOTE: This works correctly when I use my local Xamp server