简体   繁体   English

读取和编辑HttpResponseMessage的内容

[英]Read and edit the content of an HttpResponseMessage

I'm having an implementation of DelegatingHandler which basically take a request and address it to another server ( http://localhost:9999/test/page.php --> http://otherSite.com/test/page.php ). 我有一个DelegatingHandler的实现,基本上实现了一个请求并将其寻址到另一台服务器( http:// localhost:9999 / test / page.php- > http://otherSite.com/test/page.php ) 。

Following my question here , I now have to read the result of the page and edit it a little bit: 这里提出我的问题之后,我现在必须阅读页面的结果并对其进行一些编辑:

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

I'm trying to read the content of the message, change some parts of it(basically change all http://otherSite.com into http://localhost:9999 ) and then return it. 我正在尝试阅读消息的内容,更改消息的某些部分(基本上将所有http://otherSite.com更改为http:// localhost:9999 ),然后返回它。

The issues that I'm facing: 我面临的问题:

string content =await responseMessage.Content.ReadAsStringAsync();
  1. Doesn't returns me anything that is readeable(I'm not sure, but I've the impression it's maybe because of the compression?) 不会向我返回任何可读取的内容(我不确定,但是给我的印象可能是由于压缩所致?)
  2. Is there a way to replace the content of the message? 有没有办法替换消息的内容?

I'm trying to understand this a little better. 我想更好地理解这一点。 Are you simply looking to make all requests that go to that address get redirected to another address? 您是否只是想使所有发送到该地址的请求都重定向到另一个地址? Given that you're putting this in the delegating handler, I'm imagining this is being run for every incoming request. 假设您将其放入委派处理程序中,那么我想它正在为每个传入请求运行。

If that's the case, I don't really believe this is the location to write this code. 如果是这样,我真的不相信这是编写此代码的位置。 There are two other places that I think provide a cleaner solution. 我认为还有两个地方提供了更清洁的解决方案。

In the web server: 在Web服务器中:

You seem to be recreating the idea of a Reverse Proxy ( https://en.wikipedia.org/wiki/Reverse_proxy ) where you want one server to simply direct requests to another without the client knowing about the change. 您似乎正在重新创建反向代理( https://en.wikipedia.org/wiki/Reverse_proxy )的想法,在这种情况下,您希望一台服务器直接将请求定向到另一台服务器,而客户端却不知道更改。 You can make this modification on the server itself. 您可以在服务器本身上进行此修改。 If you're using IIS, there are guides to use IIS plugins like Application Request Routing to achieve this ( http://www.iis.net/learn/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing ). 如果您使用的是IIS,则有使用IIS插件(如应用程序请求路由)的指南来实现此目的( http://www.iis.net/learn/extensions/url-rewrite-module/reverse-proxy-with-url- rewrite-v2-and-application-request-routing )。 Other web servers also have their own solutions but I have not personally tried them. 其他Web服务器也有自己的解决方案,但我没有亲自尝试过。

On the route itself 在路线本身上

On whatever Web API route would be responding to this request, you can use http status codes to tell the browser to redirect the request elsewhere. 无论使用哪种Web API路由来响应此请求,都可以使用http状态代码告诉浏览器将请求重定向到其他地方。

The 3xx series were specifically made to solve this kind of problem: http://www.restapitutorial.com/httpstatuscodes.html 3xx系列专门用于解决此类问题: http : //www.restapitutorial.com/httpstatuscodes.html


That said, if you still want to use the approach in the original question , you need to ensure that there is a Media-Type Formatter for the data type that is being returned. 就是说, 如果您仍然想在原始问题中使用该方法,则需要确保对于要返回的数据类型有一个Media-Type Formatter。 By default, Web API will only attempt to use formatters for JSON, XML, and Form-url-encoded data. 默认情况下,Web API将仅尝试对JSON,XML和Form-url编码的数据使用格式化程序。 If you are returning anything else, you'll need to do a little more work ( http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters ) to get the data back to manipulate it. 如果您要返回其他任何内容,则需要做更多的工作( http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters )以获取数据回到操纵它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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