Interface Test HttpClient Practice 20150925

Source: Internet
Author: User

Using the tool to do interface testing, but for the encryption of data and results of the comparison, as well as bulk data read, back or feel the code to be more convenient and flexible, read data from the excle, constitute parameters, send requests, and get the return results and expected value comparison, and the results of the output report, can be deep into the framework, Used to be more convenient, the study of the next httpclient, just starting, is not very skilled, the following is the actual combat, do not know it does not matter, followed by practice several times, slowly understand:

1. Create a new Java project in Eclipse, add a jar package

2. Add HttpClient jar Package

3, write code, generally I will write two is a GET request, one is a POST request, I wrote here a post, which contains the data access file, and encryption decryption method:

Code area:

/**
* POST Request
*
* @param URL
* @throws IOException
*/
public static void Httpclientpostmethod (String url) throws IOException {
1. Create an instance of HttpClient
Closeablehttpclient httpClient = Httpclients.createdefault ();
POST request
HttpPost HttpPost = new HttpPost (URL);
Set Request parameters
File File = new file (
"C:/001MYWORKSPACE/001 work/01 mobile platform/02 Test Document/03 use case grooming/james use case/v0.3 Version/encrypted version/Escape after/");
file[] templist = File.listfiles ();//The method returns an array of files
for (int i = 0; i < templist.length; i++) {
Loop this array
if (Templist[i].isfile ()) {
Remove files as needed
InputStream is = null;
Closeablehttpresponse response = null;
try {
Param is a parameter, and URL encoding
String param = getdatafromtext.getdata (templist[i]
. toString ());
Stringentity entity0 = new stringentity (param);
Set Request header information
Entity0.setcontenttype ("Application/json");
Httppost.setentity (ENTITY0);
Send Request
Response = Httpclient.execute (HttpPost);
if (Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {
2. Obtain the entity of response.
httpentity entity = response.getentity ();
3. Get the InputStream object and process the content
is = Entity.getcontent ();
System.out.println (IS);
The returned content is saved to the text
SaveToFile ("c:/001myworkspace/tmp/", i + "Test.xml", is);
Get the results and decrypt
Test.getresultfromtxt ("c:/001myworkspace/tmp/" + I
+ "Test.xml");
}
} catch (Clientprotocolexception e) {
E.printstacktrace ();
} finally {
if (is = null) {
Is.close ();
}
if (response! = NULL) {
Response.close ();
}
}
}
}

}

4. Invoke, execute, and view the results in the Main method

Interface Test HttpClient Practice 20150925

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.