I'm researching a web proxy pattern that makes use of ASP.Net Core MVC project type. I'd like to basically pass a HttpRequestMessage to a httpClient in the controller which then makes a request to a remote website (like https://www.abc.fake) and then returns the response exactly as it came back from the client (body and headers). Example code:
[Microsoft.AspNetCore.Mvc.Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
//[HttpGet]
public async Task<HttpResponseMessage> Get()
{
var httpClient = new HttpClient();
var resp = await httpClient.GetAsync("https://www.abc.fake");
return resp;
}
}
The problem is every time I do this I just get a JSON serialized version of the response message. It's not actually sending back the HTML I get from the remote website. This is what I'm seeing:
{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":{"headers":[{"Key":"Content-Type","Value":["text/html; charset=utf-8"]},{"Key":"Content-Length","Value":["1036119"]},{"Key":"Expires","Value":["Wed, 23 Sep 2020 21:44:35 GMT"]},{"Key":"Last-Modified","Value":["Wed, 23 Sep 2020 21:44:35 GMT"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[{"Key":"Connection","Value":["keep-alive"]},{"Key":"Vary","Value":["Accept-Encoding","Accept-Encoding"]},{"Key":"Date","Value":["Wed, 23 Sep 2020 21:41:54 GMT"]},{"Key":"Server","Value":["nginx/1.16.1"]},{"Key":"Via","Value":["1.1 varnish-v4","1.1 e8afb729a4bc6f5676d32307ea14bdae.cloudfront.fake (CloudFront)"]},{"Key":"Accept-Ranges","Value":["bytes"]},{"Key":"Cache-Control","Value":["must-revalidate, max-age=0"]},{"Key":"Set-Cookie","Value":["SWID=0C8B6C96-3F05-43D5-C3D1-2676E1C15F8C; path=/; Expires=Sun, 23 Sep 2040 21:41:54 GMT; domain=abc.fake;"]},{"Key":"X-Cache","Value":["Miss from cloudfront"]},{"Key":"X-Amz-Cf-Pop","Value":["HIO50-C1"]},{"Key":"X-Amz-Cf-Id","Value":["yKz-d9KhZdb-5qdDpppD0jeFqYHfFQA4Z1RT98Nk31eaH7kB_FXisQ=="]}],"trailingHeaders":[],"requestMessage":{"version":{"major":1,"minor":1,"build":-1,"revision":-1,"majorRevision":-1,"minorRevision":-1},"content":null,"method":{"method":"GET"},"requestUri":"https://abc.fake/","headers":[{"Key":"Request-Id","Value":["|8e9d36f9-4b9e69ca8ec31ee9.1."]}],"properties":{}},"isSuccessStatusCode":true}
HttpResponseMessageto get to the response that gives you a json string? I'd thinkHttpResponseMessage.Content.ReadAsByteArrayAsync()would you get you the raw response. You'd need to then useEncoding.GetStringto get plain text. And it probably wouldn't include all the headers.HttpResponseMessage? Not sure if it intended to be serializable. Also better to use http client factory.