Integrate Your Java App With The Charles Web Debugging Proxy  
  September 05, 2007 filed under Tutorials, Tools  

There are very few software applications that inspire me to write. After 2 years, Charles has provided that inspiration. I came across Charles in the midst of a very hectic project. We had to mimic a browser to login into a password protected site to do screen scraping. How 1990s. But the reality is not every website provides you with an API. And as you try to mimic a browser they are a lot of gotchas that you need to be aware of. For example some servers balk if you send cookies in multiple headers as opposed to a single header. Charles allowed us to see the traffic between the browser and the site. This cut down our development time. We simply hooked our Java app with Charles and made sure that all the traffic going out was exactly the same as the browser. Quite handy when you're trying to get into multiple sites that have different security setups.

DISCLAIMER: You are following this tutorial at your own risk. If anything blows up do some meditation before assigning blame.

  What is Charles?  

Charles is a web debugging proxy running at port 8888. Installation is a snap. It is written in Java. Once fired up it automatically captures all traffic that Internet Explorer generates. It works well with Firefox also. Although free to evaluate, it will shut down every 30 minutes. Also it may show a nag screen that slows down your work for a couple of seconds every ten minutes or so. One license is $50. We ended up buying it. Here's a picture of Charles in action:

  Integrate Charles With Your Java App

1) In order to integrate with Charles do the following:

  • Download Charles and install it.
  • Download the sample Zip file and unzip it to C:\IntegrateCharles

2) The zip file consists of one src file, jar files, compile batch file, run batch file. Assuming you have Java installed the only thing that you need to do is change the path for javac in the compile bat if its different. The jar files are the famous Jakarta commons HTTP Client.


3) From a command prompt go to C:\IntegrateCharles and execute the compile.bat. Make sure you get a IntegrateCharles.class file.


4) Before you run the app you need to take care of Charles Security Certificate. When a browser does not recognize a security certificate it will prompt you and give you the option to continue or stop. E.g in IE7 you will see the following message:

In Firefox you may see:

In our case if we dont take care of the certificate it will manifest itself with the following exception: PKIX path building failed: unable to find valid certification path to requested target


Fortunately the fix is fairly simple. Copy charles.cer from C:\Program files\Charles\docs to <jre install dir>\lib\security. Then run the following command from a command prompt (make sure you are in the same folder as charles.cer):


<java install dir>/bin/keytool.exe -import -storepass changeit -file charles.cer -keystore cacerts -alias charles


You will see a bunch of stuff flying by. At the end it will ask you a question to which you answer Yes. Thats it youre done.


5) Go to C:\IntegrateCharles and run the run.bat. Make sure Charles is running. Thats it you will see traffic going through Charles



  Table Of Contents
  >>What is Charles
  >>Integrate Charles
  >>Contact Us