【问题标题】:Creating proxy for HttpRequest为 HttpRequest 创建代理
【发布时间】: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 对象?为什么对我的服务的请求似乎从未完成?

【问题讨论】:

    标签: c# asp.net proxy stream


    【解决方案1】:

    我遇到过类似的请求超时问题。上次我遇到这个问题时,它与未正确关闭连接有关,并且那里没有可用的套接字,因此整个应用程序都挂起。

    下面是我用来发送http post请求的代码sn-p。

    public void Run() {
        string url = "http://example.com/api";
        string contentType = "application/xml";
        string postData = "<this><is><my>xml</my></is></this>";
    
        string responseBody = PostRequest(url, contentType, postData);
    }
    
    
    public string PostRequest(string url, string contentType, string postData) 
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/xml";
            request.Timeout = 30000;
            request.ReadWriteTimeout = 30000;
    
            Log.Debug("HTTP request: [" + request.Method + "] " + url);
    
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
    
            var response = (HttpWebResponse) request.GetResponse();
            Log.Debug("HTTP response: " + response.StatusCode + " - " + response.StatusDescription);
    
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string responseBody = reader.ReadToEnd();
            response.Close();
    
            return responseBody;
        }
        catch (Exception e)
        {
            Log.Error("Exception while running HTTP request.", e);
            throw e;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多