IO Exception when sending large size of bytes using HttpWebRequest

  • Thread starter Thread starter HappyMaggie
  • Start date Start date
H

HappyMaggie

Guest
We got this IO exception when sending relatively bigger size of bytes to WebAPI.

The IO exception happened when sending larger size of bytes from httpwebrequest.

We are writing pure .net C# code to call a web API by using HttpWebRequest.

The code is very similar to this page:

How to post JSON to a server using C#?



code :

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"user\":\"test\"," +
"\"password\":\"bla\"}";

streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}


We just using Json.net lib to convert object to json string.

When I changed to use real large size of bytes, it throws exception as following:



Exception details:

System.IO.IOException was unhandled
HResult=-2146232800
Message=Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.
Source=System
StackTrace:
at System.Net.Sockets.NetworkStream.MultipleWrite(BufferOffsetSize[] buffers)
at System.Net.Security._SslStream.StartWriting(SplitWritesState splitWrite, SplitWriteAsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.ProcessWrite(BufferOffsetSize[] buffers, SplitWriteAsyncProtocolRequest asyncRequest)
at System.Net.TlsStream.MultipleWrite(BufferOffsetSize[] buffers)
at System.Net.PooledStream.MultipleWrite(BufferOffsetSize[] buffers)
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(String value)
at testREST_referral.Class1.Main(String[] args) in C:\Users\user.name\Documents\test\testREST_ref\testRESTl\testREST_ref\Program.cs:line 179
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
ErrorCode=10053
HResult=-2147467259
Message=An established connection was aborted by the software in your host machine
NativeErrorCode=10053
Source=System
StackTrace:
at System.Net.Sockets.Socket.MultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.MultipleWrite(BufferOffsetSize[] buffers)
InnerException:

Continue reading...
 
Back
Top