I'm writting a simple Web Proxy Application(works as IHttpHandler) which basicly exucutes incoming requests and returns response, there's how it looks:
public void ProcessRequest(HttpContext context)
var destinationUrl = GetDestinationUrlFromRequest(context);
var destinationRequest = WebRequest.Create(destinationUrl);
var destinationResponse = destinationRequest.GetResponse();
var destinationResponseStream = destinationResponse.GetResponseStream();
using (var streamReader = new StreamReader(destinationResponseStream))
var result = streamReader.ReadToEnd();
public string QueryData(ICredentials credentials)
var request = (HttpWebRequest) WebRequest.Create("myproxyapp.com/?url=urltoProxy.com");
request.Method = "POST";
request.ContentLength = 0;
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.ServicePoint.ConnectionLimit = 1;
if (credentials != null)
request.Credentials = credentials;
request.UseDefaultCredentials = true;
using (var response = (HttpWebResponse) request.GetResponse())
using (var sr = new StreamReader(response.GetResponseStream()))
Exception thrown: 'System.IO.IOException' in System.dll
Additional information: Unable to read data from the transport
connection: The connection was closed.
So I found the problem:
The server that my proxy was querying used Transfer-Encoding: chunked.
In my proxy app I already received unchunked data (that's how HTTP and .NET works).
I was copying all headers from proxy response to the client, even Transfer-Encoding: chunked header.
But, as long as my data was already unchunked, I sent unchunked data to the client.
So, the client received unchunked data and header which says to unchunk it again, which caused the problem!