Problem scenario: When debugging an application, you need to monitor the HTTP Communication between the application and the server.
Solution: the official fiddler2.android documentation mentioned that it is good to use tcpdump to obtain communication packets or real-time monitoring. fiddler2 is the best HTTP monitoring tool on windows. It has friendly interfaces and functions. fiddler2 is easy to use. After running, a proxy (127.0.0.1: 8888 by default) is created on the local machine. Other applications that access HTTPProgram, Set the proxy to the same address and port.
Implementation process:
According to the instructions in the document, when starting the simulator, use the-http-proxy parameter and set 127.0.0.1: 8888 as the online proxy of the simulator. The result fails. fiddler2 does not catch the communication response.
Solution:
Read the simulator section in the document and see the network address space section.
The simulator runs behind the virtual router/Firewall Service. This router/Firewall Service is isolated from the network interface and settings of the development machine and is also isolated from the Internet. the simulated device cannot access the development machine or other simulators on the network. what it sees is that it connects to a vro/firewall over Ethernet.
This is clear, the simulator does not know what 127.0.0.1 is, nor can it be accessed. the proxy set on 127.0.0.1 naturally cannot catch any communication response. the document goes on to write that the virtual Firewall/router management ranges from 10.0.2.2 to 10.0.2.24, the simulator IP address is 10.0.2.15, and the development machine address seen by the simulator is 10.0.2.2. that is to say, from the simulator's standpoint, fiddler2 runs on 10.0.2.2.
Therefore, the emulator command is used to add the AVD-http-proxy 10.0.2.2: 8888 parameter to start the simulator. Run browser on the simulator and access any web address. You can see that fiddler2 has caught HTTP Communication:
JavaCode:
- Private httpurlconnection createconnection (URL ){
- Httpurlconnection conn = NULL;
- Try {
- Conn = (httpurlconnection) URL. openconnection ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- Return conn;
- }
Copy code
This time, fiddler2 failed to catch communications. However, the application successfully accesses the webpage. It is not enough to add the-http-proxy parameter when starting the simulator. When the application creates a connection, you must specify the HTTP Proxy:
Java code:
- Private httpurlconnection createconnectionwithproxy (URL, string proxyaddress, int proxyport ){
- Httpurlconnection conn = NULL;
- Proxy proxy = new proxy (proxy. type. HTTP, new inetsocketaddress (proxyaddress, proxyport ));
- Try {
- Conn = (httpurlconnection) URL. openconnection (proxy );
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- Return conn;
- }
Copy code