HttpClient diagram explaining the Ming Dynasty

Source: Internet
Author: User
Tags ming response code soapui

Everyone just look at this name will want to ask what this is, in this I deliberately Baidu Wikipedia under

The HTTP protocol is probably the most widely used and most important protocol on the Internet today, and more and more Java applications need to access network resources directly through the HTTP protocol.

Although the basic functionality of the HTTP protocol has been provided in the Java NET package of the JDK. For most applications, however, the JDK library itself offers less functionality and flexibility.

HttpClient is a sub-project under Apache Jakarta Common to provide an efficient, up-to-date, feature-rich client programming toolkit that supports the HTTP protocol, and it supports the latest version numbers and recommendations for the HTTP protocol.

HttpClient has been used in a number of projects, such as the two other open source projects Cactus and htmlunit that are famous on Apache Jakarta HttpClient. Now HttpClient the latest version number is HttpClient 4.4 (GA)

I understand that the main thing is to simulate the browser to send the request to the server, listen to the response information, infer the correctness of the return result, because the browser's UI, the browser will load all the time (such as often to load the picture AH) is omitted, so this operation is very efficient

Now we go straight into combat.

Create a new project first. In Baidu search httpclient 4.4 Download the relevant JAR file

Note that 4.3 and 4.4 create requests differently.





I downloaded it from here, just to get points oh

http://download.csdn.net/detail/y515789/8470829

After decompression there are for example the following files





Copy this file to the project's Lib folder and import it


Create a new package under the SRC folder

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "height=" 651 "width=" 645 ">


Create a new class




Here first how to send a request with a mock get

First paste the original code



The first step is to instantiate the object

Closeablehttpclienthttpclient=httpclient.createdefault ();

Step two, specify URL URLs

HttpGet ("URL")

The third step is to set the information header, sometimes to, and sometimes not. This depends on the actual commissioning

These things are usually not set with default values that can also be

Htpget.setheader ("Table header name", value)

The fourth step runs the request, extracts the return information

Closeablehttpresponse Httpreponse=httpclient.execute (HttpGet);

Fifth Step

Check the response information, sometimes with the ability to check the response code. Sometimes we need to check the message.

Long Status=httpreponse.getstatusline (). Getstatuscode ();


In the example of you and me

I used to check the response code for the existence of 200 of the code, assuming that there is 200 code indicating that the return value is normal


if (status==200) {
SYSTEM.OUT.PRINTLN ("response success");
}
else {
SYSTEM.OUT.PRINTLN ("Response failed");

}



String result=entityutils.tostring (Httpreponse.getentity ());
SYSTEM.OUT.PRINTLN (result);








There's another way to check.

The text that examines the response information. Compare with the expected value, confirm whether it is normal

String result=entityutils.tostring (Httpreponse.getentity ());
SYSTEM.OUT.PRINTLN (result);


Here you can use the number of parameters, for example, when you call the Get method to pass a string to be validated. Verify that the return value is compared after this run





The POST request actually operates the same process as the GET request. Only a GET request can send a full URL at a time and post needs to put the value in the request content with the number of parameters

The difference is here.



This is to upload each of the parameters and the value one by one to the port

The complete code scale is as follows


After running





SOAPUI installation + crack diagram explain clearly
Http://blog.chinaunix.net/uid-28995070-id-5156198.html
SOAPUI How to import project files
Http://blog.chinaunix.net/uid-28995070-id-5156204.html

SOAPUI Project New--Weather forecast
Http://blog.chinaunix.net/uid-28995070-id-5156232.html

SOAPUI Test Kit NEW
Http://blog.chinaunix.net/uid-28995070-id-5156246.html




HttpClient diagram explaining the Ming Dynasty

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.