Charles debugging proxy not connecting to localhost

madphp picture madphp · Jun 1, 2016 · Viewed 10.6k times · Source

Charles Web Debugging Proxy not connecting to localhost. It was working right after I installed, but all of a sudden get connection refused, when trying to connect to localhost. Other traffic is going through proxy ok. The web server and proxy ports are different. *.8888 proxy listening on all addresses.

Get the following error

Charles Error Report

Failed to connect to remote host

Charles failed to connect to the remote host. Check that your Internet connection is ok and that the remote host is accessible. Maybe your network uses a proxy server to access the Internet? You can configure Charles to use an external proxy server in the External Proxy Settings.

The actual exception reported was:

java.net.ConnectException: Connection refused Charles Proxy, http://www.charlesproxy.com/

Answer

Anson Hwang picture Anson Hwang · Jan 7, 2017

Did you use PHP Built-in server? The FAQs of office document say:

Localhost traffic doesn't appear in Charles

Some systems are hard coded to not use proxies for localhost traffic, so when you connect to http://localhost/ it doesn't show up in Charles.

The workaround is to connect to http://localhost.charlesproxy.com/ instead. This points to the IP address 127.0.0.1, so it should work identically to localhost, but with the advantage that it will go through Charles. This will work whether or not Charles is running or you're using Charles. If you use a different port, such as 8080, just add that as you usually would, e.g. localhost.charlesproxy.com:8080.

You can also put anything in front of that domain, e.g. myapp.localhost.charlesproxy.com, which will also always resolve to 127.0.0.1.

Alternatively you can try adding a '.' after localhost, or replace localhost with the name of your machine, or use your local link IP address (eg. 192.168.1.2).

If Charles is running and you're using Charles as your proxy, you can also use local.charles as an alternative for localhost. Note that this only works when you're using Charles as your proxy, so the above approaches are preferred, unless you specifically want requests to fail if not using Charles.

If you use php built-in server use

php -S 127.0.0.1:8080 -t ./public

instead of

php -S localhost:8080 -t ./public