HttpClient 4.0 Use

Source: Internet
Author: User

Httclient Normal Process

1. Create HttpClient instances

New=  httpclients.createdefault ();

2. Create a Request instance

Newnew httppost (URL);

3. Supplementary header information (optional)

Httppost.addheader ("Referer", "http://iservice.10010.com/e4/query/basic/history_list.html"); Httppost.addheader ("Cookie", "td_cookie=18446744072103645798; mallcity=31|310; ");

4. Supplementary Request entity

1) The URL is a restful style that already contains the parameters that need to be passed in. You can skip the direct to the next action.

2) Form

New Arraylist<namevaluepair>(); Nvps.add (new basicnamevaluepair ("QueryType", "0001")); Nvps.add(new Basicnamevaluepair ("Querycode", "0001")); Nvps.add (New Basicnamevaluepair ("Billdate", "201608")) Nvps.add (new Basicnamevaluepair ("Flag", "2")); Httppost.setentity (new urlencodedformentity (Nvps));

5. Request execution

HttpResponse response == httpclient.execute (httpget); byte [] response = Httpclient.execute (Httpget,handler);

6. Read the response content

if (Response.getstatusline (). Getstatuscode () = =     response.getentity ();      if NULL ) {                     = entity.getcontent ();             Try {           // do something useful             finally  {                instream.close ( );            }        }   

7. Release the connection

The jar to be referenced

        <!--httpclient--        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpclient</artifactId>            <version>4.3.4</version>        </ Dependency>        <!--httpclient--

HttpClient 4.0 Use

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.