preface

Interface debugging is essential in the development of our application. The commonly used interface debugging methods include log printing, interceptor printing, interceptor packet capture and proxy packet capture.

Log printing and interceptor methods often require us to manually change the code or rely on tripartite libraries in the project, manual code changes are more troublesome, and it is impossible to capture multiple packages at the same time.

Interceptors capture packets in such a way that we can only capture packets for specific applications.

Of course, there is a way to install APK packet capture on the phone, but to capture packets in real time requires frequent app switching.

In view of the above, the way we work is usually using computers and mobile phones. It would be much more convenient if we could grab the bag on the computer in real time while operating the application.

Next, I introduce the method of capturing packets using the PACKET capture tool on the PC.

Introduction to the

Well-known PC capturing software include Fiddler and Charles. These two are commonly used capturing software. But when used, the interface feels more general.

In this case, I used Postman, which has a cleaner interface and is more comfortable to use with a graphical interface. You can use the packet capture software according to your personal preference. The operation is similar.

Specific packet capture method

This section describes the packet capture modes in several common scenarios

The computer and phone connect to the same WiFi

Prerequisite: The phone and computer are connected to the same network
  • Open proxy Settings for Postman

As is shown in

  1. Locate the agent Settings entry
  2. Set the port number for the proxy, which I used as the default 5555
  3. Set the folder that the network requests hold, which I have set to the default history
  4. Enable Capture Requests monitoring
  • Remember the COMPUTER’s IP address

  1. I’ll use the MAC as an example. I connected to the company’s wifi: Iot-test, and my IP address was 192.168.31.111
  • Configure the HTTP proxy on the mobile phone

  1. Turn on my phone and connect it to the same wifi as my computer. My computer was connected to iot-Test before, so my phone was also connected to iot-Test
  2. Set the host name in Proxy. The host name is the IP address of the computer in the previous step: 192.168.31.111
  3. Port number Set to the port number set in Postman, that is, 5555
  • Open the phone app you want to listen to

You can see the network request that this app made.

If you select one of these, you can see the headers message (body if any), which is automatically filled in, and click “Send” to initiate the network request again

  • filter

The above method can catch all HTTP requests on the phone, if we only want to see network requests for specific applications, we can specify URL filtering in the above method.

This allows us to see only the network requests we want.

Connect your computer to a network cable and your mobile phone to a computer hotspot

Sometimes, there is no public hot spot, so the computer connects to the Internet through the network cable, and the mobile phone connects to the hot spot set by the computer.

  • View the IP address of the hotspot set on your computer

  1. Here I set the hot spot name on my computer: Youpppp
  • View the IP address of a hotspot

The next steps are the same as in the previous scenario, except that the IP address of the host name in the proxy is changed to the IP address of the current WiFi.

The mobile phone connects to the computer through USB, and the computer connects to the Internet through cable

The first two scenarios require our computer to have a wireless network card, but sometimes our computer does not have a wireless network card when how to capture the packet?

The following describes the method of Postman+ gniRehtet +Proxifier to capture packets

We can think about what are the conditions for connecting WiFi to capture packets?

  1. The phone and computer are connected to the same WiFi, which means they are on the same network
  2. The phone does this by setting up the IP address of the computer through which the phone’s Internet requests are forwarded
  3. The packet capture software on the PC can forward and monitor the request by setting the port so as to capture the network request

There are two ways to satisfy the first condition:

  1. Cell phones use computer networks
  2. The computer uses the phone’s network

Let’s introduce method one:

Cell phones use computer networks

Step one is to turn off your phone’s network in order to use it on your computer

  • Turn off the phone’s mobile Internet and WiFi connection

The second is the mobile phone through the computer to access the Internet

  • Install Genymotion’s open source software: gnirehtet and run gnirehtet-run.cmd

An unbounded software, the client, and an HTTP server on the PC are installed on the phone. After USB connection, the client sends the network request to the server through ADB, which forwards it through its own network, and finally transmits the result back to the client through ADB.

Once this is done, the phone is connected to the Internet, and all requests from the phone are made through the computer.

Then we also need to meet the third condition, that is, set the port number to forward and monitor.

What we want to capture is all the requests made by gniRehtet, the problem is that GniRehtet is an independent client, how can its requests be forwarded through a fixed port?

  • Proxifier
  1. Setting the Proxy Server

The premiseProxifier does not enable the HTTP proxy by default. You need to find the switch to enable the HTTP proxy.

Once opened, HTTP proxies are now supported.

Next, let’s set up the HTTP proxy server. Choose Configuration Rule > Proxy Server > Add: IP address: 127.0.0.1, port: 5555.

2. Configure the proxy software

After downloading and installing the attached gniRehtet, set the application program of the agent in the “Application program” column in the figure above

3. Set the proxy rule

Configuration Rules -> Proxy Rules -> Add -> Application: Add gniRehtet. exe, action: ProxyHTTP 127.0.0.1

Other rule names are set to direct connection, so that all requests of gniRehtet will be forwarded through port 5555. After successful configuration, open the browser on the mobile phone and you can see the proxy request results displayed in Proxifier

  • In the same way as scenario 1, set port 5555 for postman

Postman will do port forwarding and listening.

Packet capture results are as follows:

Download software Resources

  1. Download postman at www.getpostman.com/
  2. Gnirehtet Open Source library: github.com/Genymobile/…
  3. The PC agent tool Proxifier

Proxifuer_setup_v3. 31. Zip link: pan.baidu.com/s/1PR6bl4gx… Extraction code: SRD1 gniRehtet-rust-Win64-v2.1. zip Link: pan.baidu.com/s/1TV-fkjn6… Extraction code: B0yl

thinking

The preceding method can only listen to HTTP requests. You need to add a trust certificate to listen to HTTPS requests. If no certificate is added, HTTPS requests cannot be detected.

Postman also supports adding certificates and, in theory, fetching HTTPS requests.

I only tried method 1 in scenario 3, but method 2 should work as well. Readers who are interested should try it on their own

other

The adb communication principle: cloud.tencent.com/developer/a…

Can easily use caught way to improve the efficiency of the developers in the debug network request and quick analysis and debugging when network attempt failed, and even the tester in the test application time also can be used when the unusual caught the problem of rapid analysis is the service or the application of the problem, improve the efficiency of problem solving