Java HTTP代理服务器 [英] Java HTTP proxy server

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

问题描述

我需要实现一个HTTP代理服务器应用程序,它将来自多个客户端的请求代理到远程服务器。

I need to implement a HTTP proxy server application which will proxy requests from multiple clients to a remote server.

以下是步骤:


  1. 代理客户转发请求

  2. 服务器代理转发请求

  3. 服务器返回代理请求

  4. 代理向客户返回请求。

  1. Client forward request to proxy
  2. Proxy forward request to server
  3. Server returns request to Proxy
  4. 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:


  1. 接受一个连接并为它启动一个线程。

  2. 阅读从客户端到空行的请求。

  3. 提取GET或CONNECT命令或其他任何命令并连接到指定的主机。

  4. 如果失败,请发送回适当的HTTP错误响应,关闭套接字,然后忘记它。

  5. 否则启动两个线程来复制字节,每个方向一个。没什么好看的,只是

  1. Accept a connection and start a thread for it.
  2. Read the request from the client up to the blank line.
  3. Extract the GET or CONNECT command or whatever it is and connect to the named host.
  4. If that fails, send back an appropriate HTTP error response, close the socket, and forget about it.
  5. 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);
}


  • 当其中一个套接字读取EOS时,关闭另一个套接字输出和退出获得EOS的线程。

  • 如果作为EOS源的套接字已经关闭输出,请关闭它们。

  • 或使用Apache SQUID。

    Or use Apache SQUID.

    这篇关于Java HTTP代理服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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