2015-10-20 10:07:57 8 Comments
I am using the HttpClient class in .Net 4.5.2 framework. I am doing a PostAsync to a third party web service. 80% of the time this post works, 20% of the time our response is cut short. In this situation we get the following exception:
System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> 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.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.FixedSizeReader.StartReading() at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.TlsStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback asyncCallback, Object asyncState) at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state) at System.Net.Http.StreamToStreamCopy.StartRead()
A subsequent identical request succeeds. It is not a request we can retry as the business has already been placed. So it leaves us in an awkward situation.
This is my code:
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = authorizationHeader;
HttpContent httpContent = new StringContent(someXml);
//Exception occurs on next line...
var response = await httpClient.PostAsync("https://thirdpartyendpoint", httpContent);
var responseXml = await response.Content.ReadAsStringAsync();
//convert to Dto
}
The third party service are successfully saving the record to their database and do not see any obvious exceptions at their end. They did note that the failing requests generally took longer (around 18-30 seconds) to write to the database than the successful requests.
Thankyou for your help
Related Questions
Sponsored Content
3 Answered Questions
0 Answered Questions
5 Answered Questions
0 Answered Questions
0 Answered Questions
2 Answered Questions
[SOLVED] Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure
- 2013-08-07 11:33:54
- john G
- 18366 View
- 3 Score
- 2 Answer
- Tags: asp.net asp.net-mvc
0 Answered Questions
PowerShell + HttpWebRequest throws exception
- 2013-11-16 08:51:10
- Alex Zhukovskiy
- 1985 View
- 0 Score
- 0 Answer
- Tags: c# .net multithreading exception powershell
0 Answered Questions
The message received was unexpected or badly formatted
- 2012-02-28 22:33:01
- aghaux
- 1847 View
- 1 Score
- 0 Answer
- Tags: c# ssl windows-server-2008 ssl-certificate
2 Answered Questions
[SOLVED] Accessing Webservice which is hosted on https server
- 2011-08-22 13:43:52
- Sathish
- 5183 View
- 0 Score
- 2 Answer
- Tags: c# .net asp.net web-services
1 Answered Questions
How do access a secure website within a sharepoint webpart?
- 2009-10-22 16:42:24
- Bill
- 1155 View
- 0 Score
- 1 Answer
- Tags: c# sharepoint web-parts
1 comments
@jonho 2015-11-03 14:32:57
we resolved this problem with 2 code changes:
Dispose of the httpResponseMessage and just work with a simple DTO
Downgrade the version of HTTP to v1.0
which has the effect of adding this Http header
rather than this
@garenyondem 2016-03-07 13:38:55
Any ideas about how to apply HttpVersion to httpClient get requests?
@jonho 2016-03-14 22:18:30
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(url)) { Version = HttpVersion.Version10, Content = httpContent }; await client.SendAsync(httpRequestMessage); should do it
@Baqer Naqvi 2016-05-18 10:35:35
worked for me.thanks