[英]Calling a web service that has list of a model as a parameter to a web form
[英]Calling a Restful Web Service that has HttpRequestMessage as a parameter?
我正在将我公司的一个 Web 服务迁移到新服务器,不幸的是,以前的开发人员让我们无法在迁移生产版本之前测试此服务的迁移。 这让我们陷入了困境,我必须制定备份计划,以防我们迁移到新服务器时出现问题。
要了解我的计划,您必须首先了解当前此 Web 服务的执行流程是:
很简单,但平台的更改已经就位,只需轻轻一按即可部署,并且开发人员不会在内部进行迁移。 因此,他们会拨动开关,让我希望迁移成功。
我有一个简单的回滚计划,其中不需要平台的开发人员回滚。 我只是在上面的链中注入一个中间人,作为平台 Web 服务的管道:
这样,如果由于某种原因,Web 服务的迁移版本失败,我可以回退到旧服务器上托管的原始版本,直到我们可以调查丢失的内容以及为什么会出错(目前我们无法做到这一点) )。
现在您已经了解了这个问题,我有一个简单的问题,就是将管道写入底层 Web 服务。 我在 web 服务中遇到了一个方法,它返回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 Web 服务并将请求消息传递给该服务?
注意:我知道,我提供的代码段将不起作用并且有错误。 我不期望任何人只是分发代码。 关于需要做什么以及为什么是我正在寻找的参考和解释。
我不确定我是否完全理解这个问题,所以如果这没有帮助,我很抱歉,但如果你的管道真的只是按原样转发每个请求,你应该能够通过将 RequestUri 属性更改为 Web 来重用传入的 HttpRequestMessage服务 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.