【发布时间】:2013-11-19 15:14:30
【问题描述】:
我有一个 Web 服务需要接收来自客户端的请求,在调整 URL 后将其转发到后端服务,然后将响应发送回客户端。
但是,当我运行它并将浏览器指向我的服务时,浏览器会坐在那里等待响应并最终超时。
如果我在调试器中单步执行该服务,它会执行一切正常并从 ProcessRequest 方法成功返回。
如果我检查 Fiddler 中的流量,我可以看到我的服务发回了正确的响应标头,但响应似乎没有正文。
我的代码是:
public class ForwardRequest : IHttpHandler
{
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext context)
{
try
{
Uri url = ConstructURL(context.Request.Url);
// Create the request object that we'll use to forward the request
HttpWebRequest forwardRequest = WebRequest.Create(url) as HttpWebRequest;
CopyRequestHeaders(context.Request, forwardRequest);
// Get the response back
HttpWebResponse response = forwardRequest.GetResponse() as HttpWebResponse;
// Copy the response back to the client
CopyResponseHeaders(response, context.Response);
using (Stream answer = response.GetResponseStream())
{
answer.CopyTo(context.Response.OutputStream);
context.Response.Flush();
}
}
catch (Exception ex)
{
...
}
finally
{
// make sure the response is closed properly.
try { context.ApplicationInstance.CompleteRequest(); }
catch { };
}
}
// Copy headers (and other info) from one response object to another
private void CopyResponseHeaders(HttpWebResponse source, HttpResponse destination)
{
foreach (var headerKey in source.Headers.AllKeys)
{
destination.Headers[headerKey] = source.Headers[headerKey];
}
destination.ContentType = source.ContentType;
}
// Copy headers (and other info) from one request object to another
// This method copied from a post on Stackoverflow.com
private void CopyRequestHeaders(HttpRequest source, HttpWebRequest destination)
{
destination.Method = source.HttpMethod;
// Copy the headers we're able to
foreach (var headerKey in source.Headers.AllKeys)
{
switch (headerKey)
{
case "Connection":
case "Content-Length":
case "Date":
case "Expect":
case "Host":
case "If-Modified-Since":
case "Range":
case "Transfer-Encoding":
case "Proxy-Connection":
// Let IIS handle these
break;
case "Accept":
case "Content-Type":
case "Referer":
case "User-Agent":
// Restricted - copied below
break;
default:
destination.Headers[headerKey] = source.Headers[headerKey];
break;
}
}
// Copy restricted headers
if (source.AcceptTypes.Any())
{
destination.Accept = string.Join(",", source.AcceptTypes);
}
destination.ContentType = source.ContentType;
if (source.UrlReferrer != null)
{
destination.Referer = source.UrlReferrer.AbsoluteUri;
}
destination.UserAgent = source.UserAgent;
// Copy content (if content body is allowed)
if (source.HttpMethod != "GET" && source.HttpMethod != "HEAD" && source.ContentLength > 0)
{
var destinationStream = destination.GetRequestStream();
source.InputStream.CopyTo(destinationStream);
destinationStream.Close();
}
}
}
我是否可能错误地将初始响应复制到 context.Response 对象?为什么对我的服务的请求似乎从未完成?
【问题讨论】: