【问题标题】:Forward HTTP request to another server将 HTTP 请求转发到另一台服务器
【发布时间】:2017-11-28 12:37:57
【问题描述】:

我有以下接收 webhook 消息的代码:

// Read posted data
string requestBody;
using (var reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
    requestBody = reader.ReadToEnd();
}
requestBody.Log();

// Attempt to forward request
context.CopyTo(Settings.Payments.Paypal.ScirraPaypalIPNEndpoint);

requestBody 包含记录的数据。然后我尝试将请求转发到另一个 URL:

    public static void CopyTo(this HttpContext source, string url)
    {
        var destination = (HttpWebRequest) WebRequest.Create(url);

        var request = source.Request;
        destination.Method = request.HttpMethod;

        // Copy unrestricted headers
        foreach (var headerKey in request.Headers.AllKeys)
        {
            if (WebHeaderCollection.IsRestricted(headerKey)) continue;
            destination.Headers[headerKey] = request.Headers[headerKey];
        }

        // Copy restricted headers
        if (request.AcceptTypes != null && request.AcceptTypes.Any())
        {
            destination.Accept = string.Join(",", request.AcceptTypes);
        }

        destination.ContentType = request.ContentType;
        destination.Referer = request.UrlReferrer?.AbsoluteUri ?? string.Empty;
        destination.UserAgent = request.UserAgent;

        // Copy content (if content body is allowed)
        if (request.HttpMethod != "GET"
            && request.HttpMethod != "HEAD"
            && request.ContentLength > 0)
        {
            using (var destinationStream = destination.GetRequestStream())
            {
                request.InputStream.Position = 0;
                request.InputStream.CopyTo(destinationStream);
                destinationStream.Close();
            }
        }

        if (!Settings.Deployment.IsLive)
        {
            ServicePointManager.ServerCertificateValidationCallback =
                (sender, certificate, chain, sslPolicyErrors) => true;
        }

        using (var response = destination.GetResponse() as HttpWebResponse)
        {
            if (response == null) throw new Exception("Failed to post to " + url);
        }
    }

接收这个转发请求的处理程序有代码:

public void ProcessRequest(HttpContext context)
{
    string requestBody;
    using (var reader = new StreamReader(HttpContext.Current.Request.InputStream))
    {
        requestBody = reader.ReadToEnd();
    }
    requestBody.Log();
}

但是在转发到的处理程序上,requestBody 始终为空!我在这里做错了什么?

【问题讨论】:

  • request.ContentLenght is 未设置。那么,愚蠢的问题,服务器是否支持 HEAD 请求?
  • @Jimi 内容长度在写入请求时自动设置,是的,目标服务器支持 HEAD、OPTION、GET、POST
  • 那么,响应的 StausCode 是什么?两者之间有代理吗?我问是因为代码中没有什么特别之处。当然,您已经尝试过 POST 传递您可以控制的数据。
  • @Jimi 感谢您的帮助 - 设法找到了问题。它只会在生产中失败,因为两台服务器都托管在 Cloudflare 中,这会导致 CF 1000 错误 - 禁止 DNS。解决方法是在源服务器hosts文件中指定目标服务器IP。

标签: c# asp.net http http-post


【解决方案1】:

两台服务器都托管在 Clouflare 中,当从一台服务器发布到另一台服务器时,我收到 CF 1000 禁止 IP 错误。

解决方案是将目标服务器 IP 地址添加到请求服务器的 hosts 文件中。

【讨论】:

    猜你喜欢
    • 2012-06-07
    • 2012-04-23
    • 2012-12-27
    • 2022-12-10
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    相关资源
    最近更新 更多