1

I have a web api controller action that sends a request to another server and gets an image.

public class MyController : ApiController
{ 
   public async Runner<HttpResponseMessage> Wms()
   {
       return await Run();                                    
   }

   private Task<HttpResponseMessage> Run()
   {
       HttpRequestMessage requestMessage = new HttpRequestMessage();
       requestMessage.RequestUri = "http://....";

       foreach (var header in this.Request.Headers)
          requestMessage.Headers.Add(header.Key, header.Value);

        return requestMessage.SendAsync();
   }
}

How can I get async request result of requestMessage.SendAsync()

1 Answer 1

1

You'll need to add the async modifier to the method and await SendAsync():

private async Task<HttpResponseMessage> RunAsync()
{
   HttpRequestMessage requestMessage = new HttpRequestMessage();
   requestMessage.RequestUri = "http://....";

   foreach (var header in this.Request.Headers)
      requestMessage.Headers.Add(header.Key, header.Value);

   HttpResponseMessage response = await requestMessage.SendAsync();
   string resultData = await response.Content.ReadAsStringAsync();
}

Or if you want the response inside Wms, you can await that.

Sign up to request clarification or add additional context in comments.

2 Comments

var response = await requestMessage.SendAsync(); response has not a propert named Result. So how can I get image result in it?
You're looking for var result = await response.Content.ReadAsStringAsync(); or ReadAsStreamAsync(), whichever you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.