简体   繁体   中英

Calling a Restful Web Service that has HttpRequestMessage as a parameter?

I am in the process of migrating one of my company's web services to a new server, and unfortunately the previous developers left us no way to test the migration of this service prior to migrating the production version. This leaves us in a harsh situation where I have to formulate a backup plan in case things go wrong when we migrate to the new server.

To understand my plan, you must first understand that the flow of execution for this web service is currently:

  1. Customer calls platform.
  2. Platform calls web service.
  3. Web service responds to platform.
  4. Platform responds to customer.

Simple enough, but the platform's changes are already in place for deployment at the flip of a switch and the developer will not be in house for the migration. Thus, they will flip the switch and leave me hoping the migration works.

I have a simple rollback plan in which the platform's developer won't be required for me to rollback. I simply inject a middle-man to the chain above which acts as a conduit to the web service for the platform:

  1. Customer calls platform.
  2. Platform calls conduit service.
  3. Conduit service calls web service.
  4. Web service responds to conduit.
  5. Conduit responds to platform.
  6. Platform responds to customer.

This way, if for some reason, the migrated version of the web service fails, I can fallback to the original version hosted on the old server until we can investigate what's missing and why it all went wrong (currently we have no way to do this).


Now that you have an understanding of the issue, I have a simple issue with writing the conduit to the underlying web service. I encountered a method in the web service that returns HttpResponseMessage and expects HttpRequestMessage as a request. This is rather confusing since the platform calls this method via the following URI:

test.domain.com:port/api/route/methodname

I have no access to the code under this URI assignment (which is in RPG code), so I have no idea how they are passing the data over. Currently my code is simple:

[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;
    }
}

How can I call a restful web service and pass on the request message to the service?

NOTE : I understand, the snippet I've supplied will not work and has an error. I DO NOT expect anyone to just hand out code. References and explanations as to what needs to be done and why are what I'm looking for.

I'm not sure I totally understand the question, so apologies if this isn't helpful, but if your conduit truly just forwards each request as-is, you should be able to reuse the incoming HttpRequestMessage by changing the RequestUri property to the web service URI and forwarding it to the web service with an instance of HttpClient. Something like this:

[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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM