0

我使用 Java 套接字创建了一个代理服务器(正向和反向)。

它将侦听在浏览器中配置的 8080 上的传入请求并将它们转发到另一个代理服务器 2。

并读取 server2 发送的 Response 并将其写入浏览器。

同时代码将记录请求和响应,并阻止来自浏览器的某些预定义请求类型。

现在我想使用 Jetty 来做这件事,并且还支持 HTTPS 请求。

我搜索了例如,但我没有找到。

这将在 8080 处启动服务器,我已在浏览器的代理设置中将其配置为代理端口。

 import org.eclipse.jetty.server.Server;

import Handler.HelloHandler;

public class StartJetty
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);

        server.setHandler(new HelloHandler());
        server.start();
        server.join();
    }
}

这是我用来监听请求并将响应写回浏览器的处理程序。

package Handler;

import java.io.IOException;

 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloHandler extends AbstractHandler
{
    final String _greeting;
          final String _body;

          public HelloHandler()
          {
              _greeting="Hello World";
              _body=null;
          }

          public HelloHandler(String greeting)
          {
              _greeting=greeting;
              _body=null;
          }

          public HelloHandler(String greeting,String body)
          {
              _greeting=greeting;
              _body=body;
          }

          public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
          {
              response.setContentType("text/html;charset=utf-8");
              response.setStatus(HttpServletResponse.SC_OK);
              baseRequest.setHandled(true);

              response.getWriter().println("<h1>"+_greeting+"</h1>");
              if (_body!=null)
                  response.getWriter().println(_body);
          }
}

一旦我得到响应,我想将其转发到代理服务器,等待它的响应并将其写回浏览器。我需要帮助。

4

1 回答 1

5

In the jetty-servlets artifact there is a ProxyServlet that will do async proxy work for you.

I would just give that a try and see if it fits your needs.

In the tests of that project is an AsyncProxyServer that you can just start up and give a whirl.

The underlying continuation and jetty clients used for the proxying are extensible through the customize methods.

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/ProxyServlet.java?h=jetty-8

and

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AsyncProxyServer.java?h=jetty-8

good luck

于 2012-03-07T13:58:35.813 回答