将HttpServletRequest转发到其他服务器 [英] Forward HttpServletRequest to a different server

查看:1746
本文介绍了将HttpServletRequest转发到其他服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Servlet中收到一个HttpServletRequest请求,我想将AS-IS(即GET或POST内容)转发到另一台服务器.

I got a HttpServletRequest request in my Spring Servlet which I would like to forward AS-IS (i.e. GET or POST content) to a different server.

使用Spring Framework最好的方法是什么?

What would be the best way to do it using Spring Framework?

我是否需要获取所有信息并构建一个新的HTTPUrlConnection?还是有更简单的方法?

Do I need to grab all the information and build a new HTTPUrlConnection? Or there is an easier way?

推荐答案

不幸的是,没有简单的方法可以做到这一点.基本上,您必须重构请求,包括:

Unfortunately there is no easy way to do this. Basically you'll have to reconstruct the request, including:

  • 正确的HTTP方法
  • 请求参数
  • 请求标头(HTTPUrlConnection不允许设置任意用户代理,始终附加"Java/1.*",您需要HttpClient)
  • 身体
  • correct HTTP method
  • request parameters
  • requests headers (HTTPUrlConnection doesn't allow to set arbitrary user agent, "Java/1.*" is always appended, you'll need HttpClient)
  • body

这是很多工作,更不用说它无法扩展了,因为每个这样的代理调用都将占用您计算机上的一个线程.

That's a lot of work, not to mention it won't scale since each such proxy call will occupy one thread on your machine.

我的建议:使用原始套接字或并拦截最低级别的HTTP协议,只需动态替换一些值(例如Host标头)即可.您能否提供更多背景信息,为什么需要它?

My advice: use raw sockets or netty and intercept HTTP protocol on the lowest level, just replacing some values (like Host header) on the fly. Can you provide more context, why so you need this?

这篇关于将HttpServletRequest转发到其他服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆