简体   繁体   English

Java代理:如何从HttpRequest中提取目标主机和端口?

[英]Java Proxy: How to extract Destination Host and Port from the HttpRequest?

I am working on a Http Proxy in java. 我正在用Java开发Http代理。 Basically I have 3 applications: 基本上我有3个应用程序:

  • a client application, where I just submit a request to a server VIA a proxy 客户端应用程序,我只是通过代理向服务器提交请求
  • a proxy that captures the request, modifies it and then forwards it to the web server 捕获请求,对其进行修改然后将其转发到Web服务器的代理
  • the web server Here is my code for the Client (taken from the apache httpcore examples, but works well): Web服务器这是我的Client代码(摘自apache httpcore示例,但效果很好):

    public class ClientExecuteProxy () { 公共类ClientExecuteProxy(){

     public static void main(String[] args)throws Exception { HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpHost target = new HttpHost("issues.apache.org", 443, "https"); HttpGet req = new HttpGet("/"); System.out.println("executing request to " + target + " via " + proxy); HttpResponse rsp = httpclient.execute(target, req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); Header[] headers = rsp.getAllHeaders(); for (int i = 0; i<headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } } 

    } }

If I do a direct execution of the request to the server (if I comment the line "httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);"), it works without any problem. 如果我直接将请求执行到服务器(如果我注释了行“ httpclient.getParams()。setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);”),那么它将正常工作。 But if I leave it like that, it will pass by the proxy. 但是,如果我这样离开,它将通过代理传递。 Here is the part that I do not know how to handle for the proxy: The proxy listens for the requests, reads its content and verifies if it respects certain policies. 这是我不知道如何处理代理的部分:代理侦听请求,读取其内容并验证其是否遵守某些策略。 If OK it will forward it to the server, else it will drop the request and it will send a HttpResponse with an error. 如果确定,它将转发给服务器,否则它将丢弃请求,并发送带有错误的HttpResponse。 The problem is when the request is OK and it needs to be forwarded. 问题是当请求确定并且需要转发时。 How does the proxy know to what address to forward it? 代理如何知道将其转发到哪个地址? My question is: How do I get the information from the request entered at the line "HttpHost target = new HttpHost("issues.apache.org", 443, "https");"? 我的问题是:如何从在“ HttpHost target = new HttpHost(“ issues.apache.org”,443,“ https”);“行中输入的请求中获取信息? I've googled for a couple of hours but found nothing. 我已经搜索了几个小时,但一无所获。 Can anybody help me please? 有人可以帮我吗?

When you define an HTTP proxy to an application or browser, either: 当您定义应用程序或浏览器的HTTP代理时,可以:

  1. There will be a preceding CONNECT request to form a tunnel, that tells you the target host:port, or 前面将有一个CONNECT请求来形成一个隧道,该隧道告诉您目标主机:端口,或

  2. The entire target URL is placed in the middle of the GET/POST/... request line. 整个目标URL放置在GET / POST / ...请求行的中间。 Normally, without a proxy, this is just a relative URL, relative to the host:port of the TCP connection. 通常,没有代理,这只是相对于TCP连接的host:port的相对URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM