Brief introduction of Http/https request Listening method in Java

Source: Internet
Author: User

First, 工欲善其事 its prerequisite

The people who do web development always avoid to deal with HTTP/HTTPS request, many times we all want to be able to visually see the request parameter that we send and the response information that the server returns, this time need to rely on some tools. This article will use Fiddler2 as an analysis tool, Fiddler is very powerful, it can record all the client and server HTTP and HTTPS requests, allowing you to monitor, set breakpoints, and even modify the input and output data, is the wall grasping the package of the weapon. For a description of the tool, refer to the following link:

Http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html

The interface of the tool is as follows (copy the above linked diagram):

Here, I would like to note the following points:

1, the principle of the tool is actually started when you opened a proxy (port is 8888, the default), when the shutdown will cancel the proxy settings. As shown, then all requests are first passed through this agent (except localhost), so it supports a variety of browsers.

2, the default is not to open the HTTPS request monitoring, so you have to manually open, tick the red box below the thing, pay attention to that paragraph of red words OH

Second, the browser request monitoring method

With the above configuration, Internet requests sent by the browser can be monitored either HTTP or HTTPS. However, one exception is that the sites on your own computer (localhost and 127.0.0.1) are not listening, because fiddler defaults to ignoring requests from these two addresses, and resolves the following methods:

1, with http://localhost. (locahost followed by a dot number)
2, with http://127.0.0.1. (127.0.0.1 followed by a dot number)
3, with Http://machinename (machine name)

Third, Java Background Engineering HTTP and HTTP request monitoring methods
1. Background Works HTTP request

By default, Fiddler cannot listen for Java httpurlconnection requests. The reason is that the Java Network Communication protocol stack may be slightly different from the browser's communication protocol stack, the principle of Fiddler listening HTTP request is to build a proxy server between the application and the operating system network communication layer. Java's httpurlconnection should have bypassed the proxy server, so fiddler could not hear the Java httpurlconnection request. The workaround is to set a proxy for the Java environment to point to Fiddler, which has two methods:

One is to set the JVM's startup parameters, and MyApp is your application name

java-dproxyset=true-dproxyhost=127.0.0.1-dproxyport=8888 MyApp

Second, set the environment properties

System.setproperty ("Http.proxyhost", "localhost"); System.setproperty ("Http.proxyport", "8888"); System.setproperty ("https.proxyhost", "localhost"); System.setproperty ("Https.proxyport", "8888");
2, the background works HTTPS request

This is more cumbersome, the whole of a long time to understand, the main difficulty is the HTTPS certificate problem, the relevant knowledge can refer to the following link: http://www.cnblogs.com/devinzhang/archive/2012/02/28/2371631.html.

We know that the SDK to send HTTPS requests must first register the certificate of the site to the JRE, the method of registration is as follows:

Where Keytool is located in the Java JDK Bin directory.

To listen to a background HTTPS request, you must register two certificates, one for the HTTPS site, and one for Fiddler's own certificate. Remember the first piece of red text above? Yes, click that button to export the certificate to the desktop. After registering the certificate for the HTTPS site, register the fiddler certificate. Then follow the above method to register the certificate, the following is the registration code of my Machine

C:\users\administrator>d:\bingoeclipse4.3win64\jdk\bin\keytool.exe-import--keystore FiddlerKeystore -Aliasfiddler

Please remember to register two certificates, otherwise there will be no error reporting the certificate.

After the above certificate registration, we should be able to implement monitoring HTTPS requests, but there is a problem, that is the previous mentioned fiddler will ignore the request of localhost, so we still want to configure a proxy point to fiddler, The following information is configured where the JVM's startup parameters are:

I use the Intell idea Editor, Eclipse is only the configuration of the place is different, personal feeling idea is more useful, smart tips are strong.

After the above configuration and registration processing, now running should be able to listen to HTTPS requests, as follows.

Now the boss no longer have to worry about my request, where not to point where ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.