[英]How can I use embedded jetty to simulate proxy server?
I have i
proxies, each with different ip host_i
. 我有i
代理,每个代理都有不同的ip host_i
。
I want to make requests to 我想提出要求
<host_i>:4444/content
go to <host_i>:4444/content
转到
localhost:9999/content?proxy=<host_i>
How do I do this? 我该怎么做呢? Here's what I have so far trying to use embedded jetty server (version: 8.1.10.v20130312), but it doesn't work and I can't figure this out. 到目前为止,这是我尝试使用嵌入式码头服务器(版本:8.1.10.v20130312)的内容,但是它不起作用,我无法弄清楚。 I got this example from: 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 我从以下示例获得了此示例: 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
proxyServer = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(4444);
proxyServer.setConnectors(new Connector[]{connector});
ServletHandler handler = new ServletHandler();
proxyServer.setHandler(handler);
ServletHolder proxy = handler.addServletWithMapping("org.eclipse.jetty.servlets.ProxyServlet", "/");
proxy.setAsyncSupported(true);
proxyServer.start();
This is my code using simpleframework that handles requests to localhost:9999
: 这是我的代码,使用simpleframework处理对localhost:9999
请求:
websiteContainer = new HttpServer();
websiteServer = new ContainerServer(websiteContainer);
websiteConnection = new SocketConnection(websiteServer);
final SocketAddress websiteAddress = new InetSocketAddress(9999);
websiteConnection.connect(websiteAddress);
public class HttpServer implements Container {
@Override
public void handle(final Request request, final Response response) {
// do some stuff here, I have a breakpoint here to test if a request to a proxy made it here
}
@Override
public void handle(final Request request, final Response response) {
// building redirect url localhost:9999/content?proxy=<host_i>
redirectUrl= "localhost:9999/" + httpRequest.getContextPath() + "? proxy =" + httpRequest.getLocalAddr();
// do remaing stuff here
}
I hope this will solve your problem as httpRequest.getContextPath()
gives you IP of where your app is running and httpRequest.getContextPath()
gives you contaxt path whish is hit 我希望这能解决您的问题,因为httpRequest.getContextPath()
为您提供应用运行的IP,而httpRequest.getContextPath()
为您提供contaxt路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.