调用以 HttpRequestMessage 作为参数的 Restful Web 服务?
Calling a Restful Web Service that has HttpRequestMessage as a parameter?
我正在将我公司的一项 Web 服务迁移到新服务器,不幸的是,以前的开发人员没有让我们在迁移生产版本之前测试此服务的迁移。这让我们处于一个严峻的境地,我必须制定一个备份计划,以防我们迁移到新服务器时出现问题。
要理解我的计划,您必须首先了解此 Web 服务的执行流程当前是:
- 客户来电平台
- 平台调用网络服务。
- Web 服务响应平台。
- 平台响应客户。
很简单,但平台的更改已经到位,只需按一下开关即可进行部署,开发人员不会在内部进行迁移。因此,他们将按下开关并让我希望迁移成功。
我有一个简单的回滚计划,平台开发人员不需要我回滚。我只是将一个中间人注入到上面的链中,它充当平台的 Web 服务的管道:
- 客户来电平台
- 平台调用管道服务。
- 管道服务调用网络服务。
- Web 服务响应管道。
- 管道响应平台。
- 平台响应客户。
这样,如果由于某种原因,Web 服务的迁移版本失败,我可以回退到旧服务器上托管的原始版本,直到我们可以调查丢失的内容以及为什么出错(目前我们有没有办法做到这一点)。
既然您已经了解了这个问题,我有一个简单的问题,就是将管道写入底层 Web 服务。我在 Web 服务中遇到了 returns HttpResponseMessage
并期望 HttpRequestMessage
作为请求的方法。这相当令人困惑,因为平台通过以下 URI 调用此方法:
test.domain.com:port/api/route/methodname
我无权访问此 URI 分配下的代码(在 RPG 代码中),因此我不知道他们如何传递数据。目前我的代码很简单:
[Route("MethodName")]
[HttpPost]
public HttpResponseMessage MethodName(HttpRequestMessage request) {
try {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{ServiceRoute}/api/route/methodname");
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response; // There is a type mismatch, I know.
} catch (Exception e) {
// Log exception.
return null;
}
}
如何调用 restful 网络服务并将请求消息传递给该服务?
注意:我知道,我提供的代码段将无法工作并且有错误。我不要 希望任何人只分发代码。关于需要做什么以及为什么我正在寻找的参考和解释。
我不确定我是否完全理解这个问题,如果这没有帮助,我们深表歉意,但如果您的管道真的只是按原样转发每个请求,您应该能够通过更改RequestUri 属性 到 Web 服务 URI,并将其转发到具有 HttpClient 实例的 Web 服务。像这样:
[Route("MethodName")]
[HttpPost]
public async HttpResponseMessage MethodName(HttpRequestMessage request) {
request.RequestUri = $"{ServiceRoute}/api/route/methodname";
request.Method = HttpMethod.Get;
request.Headers.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
//add any additional headers, etc...
try
{
//best practice is to reuse the same HttpClient instance instead of reinstantiating per request, but this will do for an example
var httpClient = new HttpClient();
var response = await httpClient.SendAsync(request);
//perform any validation or modification of the response here, or fall back to the old web service on a failure
return response;
}
catch (Exception e)
{
// Log exception.
return null;
}
}
我正在将我公司的一项 Web 服务迁移到新服务器,不幸的是,以前的开发人员没有让我们在迁移生产版本之前测试此服务的迁移。这让我们处于一个严峻的境地,我必须制定一个备份计划,以防我们迁移到新服务器时出现问题。
要理解我的计划,您必须首先了解此 Web 服务的执行流程当前是:
- 客户来电平台
- 平台调用网络服务。
- Web 服务响应平台。
- 平台响应客户。
很简单,但平台的更改已经到位,只需按一下开关即可进行部署,开发人员不会在内部进行迁移。因此,他们将按下开关并让我希望迁移成功。
我有一个简单的回滚计划,平台开发人员不需要我回滚。我只是将一个中间人注入到上面的链中,它充当平台的 Web 服务的管道:
- 客户来电平台
- 平台调用管道服务。
- 管道服务调用网络服务。
- Web 服务响应管道。
- 管道响应平台。
- 平台响应客户。
这样,如果由于某种原因,Web 服务的迁移版本失败,我可以回退到旧服务器上托管的原始版本,直到我们可以调查丢失的内容以及为什么出错(目前我们有没有办法做到这一点)。
既然您已经了解了这个问题,我有一个简单的问题,就是将管道写入底层 Web 服务。我在 Web 服务中遇到了 returns HttpResponseMessage
并期望 HttpRequestMessage
作为请求的方法。这相当令人困惑,因为平台通过以下 URI 调用此方法:
test.domain.com:port/api/route/methodname
我无权访问此 URI 分配下的代码(在 RPG 代码中),因此我不知道他们如何传递数据。目前我的代码很简单:
[Route("MethodName")]
[HttpPost]
public HttpResponseMessage MethodName(HttpRequestMessage request) {
try {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{ServiceRoute}/api/route/methodname");
request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response; // There is a type mismatch, I know.
} catch (Exception e) {
// Log exception.
return null;
}
}
如何调用 restful 网络服务并将请求消息传递给该服务?
注意:我知道,我提供的代码段将无法工作并且有错误。我不要 希望任何人只分发代码。关于需要做什么以及为什么我正在寻找的参考和解释。
我不确定我是否完全理解这个问题,如果这没有帮助,我们深表歉意,但如果您的管道真的只是按原样转发每个请求,您应该能够通过更改RequestUri 属性 到 Web 服务 URI,并将其转发到具有 HttpClient 实例的 Web 服务。像这样:
[Route("MethodName")]
[HttpPost]
public async HttpResponseMessage MethodName(HttpRequestMessage request) {
request.RequestUri = $"{ServiceRoute}/api/route/methodname";
request.Method = HttpMethod.Get;
request.Headers.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
//add any additional headers, etc...
try
{
//best practice is to reuse the same HttpClient instance instead of reinstantiating per request, but this will do for an example
var httpClient = new HttpClient();
var response = await httpClient.SendAsync(request);
//perform any validation or modification of the response here, or fall back to the old web service on a failure
return response;
}
catch (Exception e)
{
// Log exception.
return null;
}
}