nevets1219
nevets1219

Reputation: 7706

Configuring Eclipse to allow Fiddler to intercept requests

In Eclipse there are two places that I've attempted to configure so that Fiddler can intercept the HTTP/HTTPS requests that I'm sending out:

  1. Windows > Preference > General > Network Connections - I've tried Native/Direct/Manual
  2. In VM arguments, I add the following -DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888

EDIT: I've tried the new properties suggested by rgerganov as well.

I haven't touched any "network" related settings in Fiddler and I've set it to monitor all processes.

I tried with Wireshark and I was able to intercept the requests with no modifications to Eclipse but information presented in Wireshark is too in-depth and I don't need most of the details provided by Wireshark.

EDIT: Here's the sample code which I'm trying:

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();
    }
  }

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

Please let me know if you need any other information.

Upvotes: 12

Views: 22808

Answers (5)

jdramaix
jdramaix

Reputation: 1104

You have many possibility to configure proxy in java :

  1. Using system properties
    • via VM arguments (assuming you want to enable proxy for http and https traffic) :

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

  • via System properties :

    // 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");   
    

The "disavdantage" of this method : once the proxy is set for a protocol, all connection for that protocol will use the proxy

  1. java.net.Proxy : available since java 1.5. Allow you to specify a proxy configuration to use for opening a connection

    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();
    
  2. ProxySelector class : available since Java 1.5. allow you to select a proxy by URI ! It's an abstract class, a simple implementation returning your proxy for all http and https connection looks like :

    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...
        }
    }
    

And set your proxy selector as default proxy selector

    public static void main(String[] args){
     ProxySelector.setDefault(new MyProxySelector());
     //...
    }

I prefer this last mechanism because you can put some logging and see wath Java is doing !

Important remark :

By default, Fiddler captures only traffic coming from web-browsers !! Change that to capture traffic of all processes : bottom toolbar
(source: comexis.eu)

Upvotes: 5

Jackie
Jackie

Reputation: 25969

JVM parameter of proxy host and proxy port can be used. At the same time, Fiddler certificate needs to be installed to sniffer https connections.

Upvotes: 1

nevets1219
nevets1219

Reputation: 7706

HttpClient needs to be made aware of the proxy information. Several approaches can be used:

See 2.7 in HTTP Component's documentation:

  1. "Connect to the target host via a proxy is by setting the default proxy parameter"

    DefaultHttpClient httpclient = new DefaultHttpClient();
    
    HttpHost proxy = new HttpHost("127.0.0.1", 8888);
    
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
  2. Using "the standard JRE proxy selector to obtain proxy information"

    DefaultHttpClient httpclient = new DefaultHttpClient();
    
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    
    httpclient.setRoutePlanner(routePlanner);
    

    and then adding the following as VM arguments:

    -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888
    
  3. Custom RoutePlanner implementation (I did not explore this option)

Upvotes: 16

Steven Mai
Steven Mai

Reputation: 63

Also, in Fiddler if you need to capture All Processes from a HTTPS connection, then you need to go to the Tools menu - Fiddler Options - HTTPS - check the 'Decrypt HTTPS traffic' and select '...from all processes'.

Selecting the 'All Processes' from Fiddler's status bar apparently is just for HTTP connections.

Upvotes: 2

rgerganov
rgerganov

Reputation: 2232

The second method should work, however the properties are http.proxyHost and http.proxyPort.

Upvotes: 2

Related Questions