1 PackageLoadRunner;2 3 Importorg.apache.http.HttpEntity;4 ImportOrg.apache.http.HttpResponse;5 ImportOrg.apache.http.client.methods.HttpGet;6 Importorg.apache.http.impl.client.CloseableHttpClient;7 Importorg.apache.http.impl.client.HttpClients;8 9 ImportJava.io.BufferedReader;Ten One ImportJava.io.InputStreamReader; A - /* - * The steps to send get and POST requests using Apache HttpClient are as follows: the * 1. Use the Help class httpclients to create the Closeablehttpclient object. - * 2. Create a httpget or HttpPost instance based on the type of HTTP request you want to send . - * 3. Use the AddHeader method to add the request header, such as User-agent, accept-encoding and other parameters. - * 4. For a POST request, create a Namevaluepair list and add all the form parameters. Then fill it into the httppost entity. + * 5. Obtain the Closeablehttpresponse instance by performing this httpget or HttpPost request - * 6. Get status codes, error messages, response pages, and so on from this closeablehttpresponse instance. + * 7. Finally close the HttpClient resource. A * */ at - /** - * Using the HttpClient call interface - * Write interface script for performance test - */ - Public classGetchannelline { in Public Static voidMain (String args[])throwsException { -String Channelid = "SDD"; toString clientId = "123"; + //Destination Address -String url = "http://XXX.XXX.com.cn/arowana/channel/getChannelLine?channelId=" + channelid + "&clientid=" +clientId; theHttpGet HttpGet =Newhttpget (URL); * $ //set Type "application/x-www-form-urlencoded" "Application/json"Panax NotoginsengHttpget.setheader ("Content-type", "application/x-www-form-urlencoded"); -System.out.println ("Call URL:" +Httpget.geturi ()); the + //httpclient Instantiation ACloseablehttpclient httpClient =Httpclients.createdefault (); the //executes the request and gets the return +HttpResponse response =Httpclient.execute (httpget); - //System.out.println ("Response toString ()" + response.tostring ()); $httpentity entity =response.getentity (); $System.out.println ("Return Status code:" +response.getstatusline ()); - - //gets the length of the returned data; no this parameter returns-1 the //if (entity! = null) { - //System.out.println ("return message Content Length:" + entity.getcontentlength ());Wuyi // } the - //Show Results WuBufferedReader reader =NewBufferedReader (NewInputStreamReader (Entity.getcontent (), "UTF-8")); -String line =NULL; AboutStringBuffer Responsesb =NewStringBuffer (); $ while(line = Reader.readline ())! =NULL) { - Responsesb.append (line); - } -SYSTEM.OUT.PRINTLN ("return message:" +responsesb); A reader.close (); + the httpclient.close (); - } $}
Dependent Packages:
1 <Dependency>2 <groupId>Org.apache.httpcomponents</groupId>3 <Artifactid>HttpClient</Artifactid>4 <version>4.5.5</version>5 </Dependency>
Java uses httpclient to send a GET request