Java uses httpclient to send a GET request

Source: Internet
Author: User

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

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.