Max Dove Max Dove - 5 months ago 29
C# Question

C# Simple Web Proxy fails with "Unable to read data from the transport connection"

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);
CopyHeadersFromTo(context.Request, destinationRequest);
var destinationResponse = destinationRequest.GetResponse();
CopyHeadersFromTo(destinationResponse, context.Response);
var destinationResponseStream = destinationResponse.GetResponseStream();
using (var streamReader = new StreamReader(destinationResponseStream))
var result = streamReader.ReadToEnd();

Also, I have another small application, which queries the one from above to get response:

public string QueryData(ICredentials credentials)
var request = (HttpWebRequest) WebRequest.Create("");

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()))
return sr.ReadToEnd();

And on sr.ReadToEnd() I keep getting this:

Exception thrown: 'System.IO.IOException' in System.dll

Additional information: Unable to read data from the transport
connection: The connection was closed.

Can anyone help with this problem, please?


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!