配置Eclipse以允许Fiddler拦截请求

nev*_*219 12 eclipse fiddler

在Eclipse中,有两个地方我试图配置,以便Fiddler可以拦截我发送的HTTP/HTTPS请求:

  1. Windows > Preference > General > Network Connections - 我尝试过Native/Direct/Manual
  2. 在VM参数中,我添加以下内容 -DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888

编辑:我也尝试了rgerganov建议的新属性.

我没有触及Fiddler中的任何"网络"相关设置,我将其设置为监控所有进程.

我尝试使用Wireshark,我能够在不修改Eclipse的情况下拦截请求,但Wireshark中提供的信息太深入了,我不需要Wireshark提供的大部分细节.

编辑:这是我正在尝试的示例代码:

public static void doPOST() {
    String post_url = "https://lookup.mxtelecom.com/USLookup";

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion( params, HttpVersion.HTTP_1_1 );
    HttpProtocolParams.setContentCharset( params, "UTF-8" );
    HttpProtocolParams.setUseExpectContinue( params, true );

    SchemeRegistry supportedSchemes = new SchemeRegistry();
    supportedSchemes.register( new Scheme( "https", SSLSocketFactory.getSocketFactory(), 443 ) );
    supportedSchemes.register( new Scheme( "http", PlainSocketFactory.getSocketFactory(), 80 ) );

    ClientConnectionManager ccm = new ThreadSafeClientConnManager( params, supportedSchemes );
    HttpClient m_Client = new DefaultHttpClient( ccm, params );

    HttpPost httpPost = new HttpPost( post_url );

    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add( new BasicNameValuePair( "something", "useful" ) );

    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
      @Override
      public String handleResponse( HttpResponse response ) throws ClientProtocolException, IOException {
        if ( response.getEntity() != null ) {
          return EntityUtils.toString( response.getEntity() );
        }

        return null;
      }
    };

    try {
      UrlEncodedFormEntity entity = new UrlEncodedFormEntity( postParameters, "UTF-8" );
      httpPost.setEntity( entity );
      results = m_Client.execute( httpPost, responseHandler );

    } catch ( ClientProtocolException e ) {
      e.printStackTrace();
    } catch ( IOException e ) {
      e.printStackTrace();
    }
  }
Run Code Online (Sandbox Code Playgroud)

Eclipse Build id:20100218-1602 // 3.5.2.R35x
Fiddler2 v2.3.2.6
jdk1.6.0_21

如果您需要任何其他信息,请与我们联系.

nev*_*219 16

HttpClient需要了解代理信息.可以使用几种方法:

请参阅HTTP组件文档中的 2.7 :

  1. "通过代理连接到目标主机是通过设置默认代理参数"

    DefaultHttpClient httpclient = new DefaultHttpClient();
    
    HttpHost proxy = new HttpHost("127.0.0.1", 8888);
    
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用"标准JRE代理选择器获取代理信息"

    DefaultHttpClient httpclient = new DefaultHttpClient();
    
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    
    httpclient.setRoutePlanner(routePlanner);
    
    Run Code Online (Sandbox Code Playgroud)

    然后将以下内容添加为VM参数:

    -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888
    
    Run Code Online (Sandbox Code Playgroud)
  3. 自定义RoutePlanner实现(我没有探索此选项)


jdr*_*aix 5

您有很多可能在java中配置代理:

  1. 使用系统属性
    • 通过VM参数(假设您要为http和https流量启用代理):

java -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888

  • 通过系统属性:

    // Get system properties
    Properties sysProperties = System.getProperties();
    // Specify proxy settings
    sysProperties.put("https.proxyHost", "127.0.0.1");
    sysProperties.put("https.proxyPort", "8888");
    sysProperties.put("http.proxyHost", "127.0.0.1");
    sysProperties.put("http.proxyPort", "8889");   
    
    Run Code Online (Sandbox Code Playgroud)

此方法的"disavdantage":一旦为协议设置了代理,该协议的所有连接都将使用代理

  1. java.net.Proxy:自java 1.5起可用.允许您指定用于打开连接的代理配置

    SocketAddress addr = new  InetSocketAddress("127.0.0.1", 8888);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
    URL url = new URL("http://stackoverflow.com/");
    InputStream in = url.openConnection(proxy).getInputStream();
    
    Run Code Online (Sandbox Code Playgroud)
  2. ProxySelector类:自Java 1.5起可用.允许您通过URI选择代理!它是一个抽象类,一个简单的实现,返回所有http和https连接的代理,如下所示:

    public static class MyProxySelector extends ProxySelector {
        private List<Proxy> proxy;
        private List<Proxy> noProxy;
    
        MyProxySelector() {
            proxy = new ArrayList<Proxy>();
            SocketAddress addr = new  InetSocketAddress("127.0.0.1", 8888);
            proxy.add(new Proxy(Proxy.Type.HTTP, addr));
    
            noProxy = new ArrayList<Proxy>();
            noProxy.add(Proxy.NO_PROXY);
        }
    
        public List<Proxy> select(URI uri) {
            System.err.println("Connection to the proxy for uri :"+uri);
            if (uri == null) {
                throw new IllegalArgumentException("URI can't be null.");
            }
            String protocol = uri.getScheme();
    
            if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
                //if http or https connection, return our proxy config
                return proxy;
            } else {
                // Otherwise don't use a proxy
                return noProxy;
            }
        }
    
        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            System.err.println("Connection to the proxy failed !");
            //throw exception or do some stuff...
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

并将代理选择器设置为默认代理选择器

    public static void main(String[] args){
     ProxySelector.setDefault(new MyProxySelector());
     //...
    }
Run Code Online (Sandbox Code Playgroud)

我更喜欢这种最后一种机制,因为你可以放一些日志记录,看看Java正在做什么!

重要说明:

默认情况下,Fiddler仅捕获来自网络浏览器的流量!! 更改以捕获所有进程的流量:底部工具栏http://julien.comexis.eu/fiddler.jpg