C# 简单 Web 代理失败并显示 "Unable to read data from the transport connection"
C# Simple Web Proxy fails with "Unable to read data from the transport connection"
我正在编写一个简单的 Web 代理应用程序(作为 IHttpHandler 工作),它基本上执行传入请求和 returns 响应,它的外观如下:
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();
}
此外,我还有另一个小应用程序,它查询上面的应用程序以获得响应:
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();
}
}
}
在 sr.ReadToEnd() 上,我不断得到这个:
Exception thrown: 'System.IO.IOException' in System.dll
Additional information: Unable to read data from the transport
connection: The connection was closed.
有人可以帮忙解决这个问题吗?
所以我发现了问题:
我的代理正在查询的服务器使用 Transfer-Encoding: chunked.
在我的代理应用程序中,我已经收到了未分块的数据(这就是 HTTP 和 .NET 的工作方式)。
我正在将代理响应中的所有 header 复制到客户端,甚至 Transfer-Encoding: chunked header.
但是,只要我的数据已经未分块,我就将 未分块 数据发送给客户端。
所以,客户端收到了未分块的数据,header 说要再次分块,这导致了问题!
我正在编写一个简单的 Web 代理应用程序(作为 IHttpHandler 工作),它基本上执行传入请求和 returns 响应,它的外观如下:
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();
}
此外,我还有另一个小应用程序,它查询上面的应用程序以获得响应:
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();
}
}
}
在 sr.ReadToEnd() 上,我不断得到这个:
Exception thrown: 'System.IO.IOException' in System.dll
Additional information: Unable to read data from the transport connection: The connection was closed.
有人可以帮忙解决这个问题吗?
所以我发现了问题:
我的代理正在查询的服务器使用 Transfer-Encoding: chunked.
在我的代理应用程序中,我已经收到了未分块的数据(这就是 HTTP 和 .NET 的工作方式)。
我正在将代理响应中的所有 header 复制到客户端,甚至 Transfer-Encoding: chunked header.
但是,只要我的数据已经未分块,我就将 未分块 数据发送给客户端。
所以,客户端收到了未分块的数据,header 说要再次分块,这导致了问题!