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();
context.Response.Write(result);
streamReader.Close();
}
context.Response.End();
}
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("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;
}
else
{
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!