简体   繁体   English

com.sun.net.httpserver.httpserver 的可修改请求标头

[英]Modifiable request headers with com.sun.net.httpserver.httpserver

I am creating a simple and lightweight reverse HTTP proxy and using com.sun.net.httpserver.httpserver for it.我正在创建一个简单而轻量级的反向 HTTP 代理,并为其使用com.sun.net.httpserver.httpserver

Once the proxy is done with its work it should add few headers in request headers and forward the request to actual service, however when i add anything in request headers i get UnsupportedOperationException because it is using UnmodifiableHeaders .代理完成其工作后,它应该在请求标头中添加一些标头并将请求转发到实际服务,但是当我在请求标头中添加任何内容时,我得到 UnsupportedOperationException 因为它使用的是UnmodifiableHeaders

Can someone tell me how to configure com.sun.net.httpserver.httpserver to use modifiable headers?有人能告诉我如何配置 com.sun.net.httpserver.httpserver 以使用可修改的标头吗?

Or is there any other better yet super lightweight HTTP server which supports this?或者有没有其他更好但超轻量级的 HTTP 服务器支持这个?

java.lang.UnsupportedOperationException: unsupported operation
    at jdk.httpserver/sun.net.httpserver.UnmodifiableHeaders.add(UnmodifiableHeaders.java:65)
    at com.example.SampleFilter.lambda$addValuesInRequestHeader$1(SampleFilter.java:80)

There is no way to add/edit headers but we can use httpExchange.setAttribute() and httpExchange.getAttribute() methods to pass additional information when hopping between filters.无法添加/编辑标头,但我们可以使用 httpExchange.setAttribute() 和 httpExchange.getAttribute() 方法在过滤器之间跳转时传递附加信息。

For adding headers to the request that we want to forward to service, we create a new http request from the original one and include attributes in headers为了向我们要转发给服务的请求添加标头,我们从原始请求创建一个新的 http 请求,并在标头中包含属性

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

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