System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at CanadianTireDashboard.Controllers.HomeController.<pickUpApi>d__109.MoveNext() in C:\Users\sbaqai\Documents\TFS\CanadianTireDashboard\CanadianTireDashboard\Controllers\HomeController.cs:line 865
I have given up. Idk why this works on our dev server but not on prod. Our dev and prod are on IIS. I am still new to C# and .NET Framework but after Googling around I still can't fix this error. Nothing works.
Here is my code that is trying to call HttpClient.PostAsync
private async Task pickUpApi(string billOfLading)
{
ErrorFile f = new ErrorFile();
HomeController hc = new HomeController();
HttpResponseMessage pickUpRequestCall = null;
//Send a request to url with an xml body
try
{
//var url = "http://www.saiasecure.com/webservice/shipment/xml.aspx?";
var url = "https://www.tst-cfexpress.com/xml/pickup";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(pickuprequest));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
pickuprequest cdn = new pickuprequest();
var values = cdn.getAll(billOfLading);
try
{
var path = $@"C:\CDNTireJobs\Test\{billOfLading}.xml";
var encoding = Encoding.GetEncoding("ISO-8859-1");
using (XmlTextWriter tw = new XmlTextWriter(path, encoding))
{
xmlSerializer.Serialize(tw, values, ns);
tw.Close();
}
}
catch (FileNotFoundException e)
{
f.errorFile($"File Exception {e.Message}");
}
var xmlcontent = System.IO.File.ReadAllText($@"C:\CDNTireJobs\Test\{billOfLading}.xml");
var content = new StringContent(xmlcontent,
Encoding.GetEncoding("ISO-8859-1"), "application/xml");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
pickUpRequestCall = await httpClient.PostAsync(url, content);
f.errorFile(pickUpRequestCall.StatusCode.ToString());
if (pickUpRequestCall.StatusCode == HttpStatusCode.OK)
{
var strResBody = pickUpRequestCall.Content.ReadAsStringAsync().Result;
f.errorFile(strResBody);
}
}
catch(HttpRequestException e)
{
f.errorFile(e.ToString());
}
}
I tried using
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.