Java HTTP代理服务器 [英] Java HTTP proxy server
问题描述
我需要实现一个HTTP代理服务器应用程序,它将来自多个客户端的请求代理到远程服务器。
I need to implement a HTTP proxy server application which will proxy requests from multiple clients to a remote server.
以下是步骤:
- 代理客户转发请求
- 服务器代理转发请求
- 服务器返回代理请求
- 代理向客户返回请求。
- Client forward request to proxy
- Proxy forward request to server
- Server returns request to Proxy
- Proxy returns request to Client.
我只是不确定如何我应该实现这个代理。我的第一个想法是实现一个tomcat应用程序,它使用jersey / apache httpclient将请求转发给远程服务器并将响应返回给客户端?
I'm just not sure how I should implement this proxy. My first thought was to implement a tomcat application which uses jersey / apache httpclient to forward the request to the remote server and return the response back to the client ?
是否有更好的方法来实现这样的代理服务器?
Is there a better way to implement such a proxy server ?
代理需要处理多个线程。
The proxy would need to handle multiple threads.
推荐答案
您无法将其实现为servlet,并且没有理由使用任何形式的HTTP客户端。
You can't implement it as a servlet, and there is no reason to use any form of HTTP client.
无特征代理服务器这是一件非常简单的事情:
A featureless proxy server is a really simple thing:
- 接受一个连接并为它启动一个线程。
- 阅读从客户端到空行的请求。
- 提取GET或CONNECT命令或其他任何命令并连接到指定的主机。
- 如果失败,请发送回适当的HTTP错误响应,关闭套接字,然后忘记它。
-
否则启动两个线程来复制字节,每个方向一个。没什么好看的,只是
- Accept a connection and start a thread for it.
- Read the request from the client up to the blank line.
- Extract the GET or CONNECT command or whatever it is and connect to the named host.
- If that fails, send back an appropriate HTTP error response, close the socket, and forget about it.
Otherwise start two threads to copy bytes, one in each direction. Nothing fancy, just
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
或使用Apache SQUID。
Or use Apache SQUID.
这篇关于Java HTTP代理服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!