DelegatingHandler
的实现,它基本上接受请求并将其转发到另一台服务器(http://localhost:9999/test/page.php -->http://otherSite.com/test/page.php) 。在我的问题这里后,我现在必须读取页面的结果并稍微编辑一下。
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
string url = request.RequestUri.PathAndQuery;
UriBuilder forwardUri = new UriBuilder(_otherWebSiteBase);
forwardUri.Path = url;
HttpRequestMessage newRequest = request.Clone(forwardUri.Uri.ToString());
HttpResponseMessage responseMessage = await _client.SendAsync(newRequest);
//HERE: How to read the responseMessage.Content in string?
return responseMessage;
}
我正在尝试阅读消息的内容,并更改其中的一些部分(基本上将所有http://otherSite.com更改为http://localhost:9999),然后返回它。
我面临的问题:
string content =await responseMessage.Content.ReadAsStringAsync();
- 没有返回任何可读的东西(我不确定,但我有印象可能是由于压缩造成的?)
- 有没有办法替换消息的内容?
text/html
,但内容无法读取(一些未识别的字符)。我在响应头中看到了gzip,所以我需要先解压缩它吗?还是表明我不支持它? - J4N