使用Vertx HttpProxy做反向代理时POST表单参数丢失该如何解决
Vert.x HttpProxy POST表单参数丢失问题解决方案
问题根源
- 你调试时观察到的
HttpServerRequest.params属性为null属于正常现象:该属性仅存储URL路径后的查询参数,POST表单默认提交的application/x-www-form-urlencoded类型参数位于请求体中,对应存放在HttpServerRequest.formAttributes属性内,因此params为空不是参数丢失的原因。 - 核心问题来自Vert.x HttpServer的默认配置:浏览器发送POST表单请求时会默认携带
Expect: 100-continue头,等待服务端返回100状态码后再发送实际的请求体内容。你当前的配置未开启100-continue自动处理,导致浏览器不会发送请求体,代理转发到源站的请求自然没有表单参数。
修复方案
步骤1:修改HttpServer配置,开启100-continue自动处理
在原有HttpServerOptions配置段新增如下配置:
options.setHandle100ContinueAutomatically(true);
步骤2(可选,适配Vert.x 4.2及更早版本):使用Builder模式创建HttpProxy实例
替换原有创建HttpProxy的代码,确保转发逻辑完整性:
HttpProxy proxy = HttpProxy.builder() .withHttpClient(client) .origin(8080, "localhost") .build();
修复后完整代理配置参考:
@Override public void start() throws Exception { HttpServerOptions options = new HttpServerOptions(); HttpClientOptions clientopt = new HttpClientOptions(); options.setMaxHeaderSize(6000); options.setMaxInitialLineLength(100000); options.setTcpKeepAlive(true); // 新增配置:自动处理100-continue请求 options.setHandle100ContinueAutomatically(true); clientopt.setMaxHeaderSize(6000); clientopt.setMaxInitialLineLength(100000); clientopt.setTcpKeepAlive(true); HttpClient client = vertx.createHttpClient(clientopt); HttpProxy proxy = HttpProxy.builder() .withHttpClient(client) .origin(8080,"localhost") .build(); HttpServer server = vertx.createHttpServer(options); server.requestHandler(proxy); server.listen(8010,result->{ if(result.succeeded()) System.out.println("Proxy Server has started!"); else if(result.failed()) System.out.println("Proxy Server start FAILED. Reason: "+result.cause()); }); }
修改完成后重启代理服务即可正常转发POST表单参数。
内容的提问来源于stack exchange,提问作者Factor Three
相关产品推荐
相关产品推荐

