Web Api 2 - 转发请求不起作用

时间:2016-04-29 14:59:02

标签: c# asp.net-web-api2

我正在实施一个只发布到公共IP的支付系统,这会产生开发问题。

因此我在UAT服务器上托管一个代理控制器,接受请求并将其转发到相应的开发机器。

所以控制器有点像这样:

    public async Task<IHttpActionResult> ListenerProxy([FromUri] DepositResponse response)
    {
        // If request is targetted towards a developer machine, reroute the request to that machine directly.
        if (response.customField1 == "localhost")
        {
            // Re-route to  machine name
            using (var httpClient = new HttpClient())
            {
                var forwardedQuery = Request.RequestUri.Query.Replace("customField1=localhost", "customField1=forwarded");
                var localPath = Request.RequestUri.LocalPath.Replace("/api", "/myapi");
                string absoluteUrl = $"http://{response.customField2}{localPath}{forwardedQuery}";
                var proxyRequest = new HttpRequestMessage(Request.Method, absoluteUrl);
                foreach (var header in Request.Headers)
                {
                    proxyRequest.Headers.Add(header.Key, header.Value);
                }

                await httpClient.SendAsync(proxyRequest); //Doesn't work
                return ResponseMessage(new HttpResponseMessage(HttpStatusCode.Moved));
            }
        }

        return await StartDeposit(response);
    }

此时absoluteUrl是正确的,如果我抓住它的值并将其直接添加到我的浏览器上,则会在本地处理请求而不会出现问题。

但是,使用httpClient.SendAsync(proxyRequest)以编程方式执行此操作不会。有什么想法吗?

0 个答案:

没有答案