可修改的请求标头,使用com.sun.net.httpserver.HttpServer

huangapple go评论75阅读模式
英文:

Modifiable request headers with com.sun.net.httpserver.httpserver

问题

我正在创建一个简单轻巧的反向HTTP代理,并使用com.sun.net.httpserver.httpserver来实现。

代理完成工作后,应在请求标头中添加一些标头,并将请求转发到实际服务,然而,当我在请求标头中添加任何内容时,会出现UnsupportedOperationException,因为它使用UnmodifiableHeaders

有人能告诉我如何配置com.sun.net.httpserver.httpserver以使用可修改的标头吗?

或者是否有其他更好且超轻量级的支持此功能的HTTP服务器?

java.lang.UnsupportedOperationException:不支持的操作
	at jdk.httpserver/sun.net.httpserver.UnmodifiableHeaders.add(UnmodifiableHeaders.java:65)
	at com.example.SampleFilter.lambda$addValuesInRequestHeader$1(SampleFilter.java:80)
英文:

I am creating a simple and lightweight reverse HTTP proxy and using com.sun.net.httpserver.httpserver for it.

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.

Can someone tell me how to configure com.sun.net.httpserver.httpserver to use modifiable headers?

Or is there any other better yet super lightweight HTTP server which supports this?

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)

答案1

得分: 0

无法添加/编辑标头,但我们可以使用httpExchange.setAttribute()和httpExchange.getAttribute()方法在跳转到过滤器之间传递附加信息。

要向要转发到服务的请求添加标头,我们从原始请求创建一个新的http请求,并在标头中包含属性。

英文:

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.

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

huangapple
  • 本文由 发表于 2020年8月24日 01:06:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63549807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定