Apache httpclient Component Encapsulation Tool class

Source: Internet
Author: User

Package com.mengyao.spider.utils;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Map.Entry;

Import Org.apache.http.Header;
Import org.apache.http.HttpEntity;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.CloseableHttpResponse;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpPost;
Import Org.apache.http.client.methods.HttpRequestBase;
Import org.apache.http.impl.client.CloseableHttpClient;
Import Org.apache.http.impl.client.HttpClientBuilder;
Import org.apache.http.impl.client.HttpClients;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.util.EntityUtils;
Import Org.apache.http.NameValuePair;

/**
* Dependent on Httpclient-4.4.jar, Httpcore-4.4.jar, Commons-logging-1.2.jar in the httpclient component of the Apache Httpcomponents project
* @author Mengyao
*
*/
public class Httputil {

/**
* Create HttpClient instances
* @return
*/
Public Closeablehttpclient gethttpclient () {
Httpclientbuilder builder = Httpclients.custom ();
Closeablehttpclient client = Builder.build ();
return client;
}

/**
* Construct POST request
* @param URL
* @param params
* @param encoding
* @return
* @throws Exception
*/
Public httppost gethttppost (string url, map<string, string> params, string encoding) throws exception{
HttpPost post = new HttpPost (URL);
list<namevaluepair> parammeters = new arraylist<namevaluepair> ();
iterator<entry<string, string>> Iterator = Params.entryset (). Iterator ();
while (Iterator.hasnext ()) {
Entry<string, string> next = Iterator.next ();
Parammeters.add (New Basicnamevaluepair (Next.getkey (), Next.getvalue ()));
}
Urlencodedformentity urlencodedformentity = new Urlencodedformentity (parammeters, encoding);
Post.setentity (urlencodedformentity);

return post;
}

/**
* Construct GET Request
* @param URL
* @return
*/
Public httpget gethttpget (String URL) {
HttpGet get = new HttpGet (URL);
return get;
}

/**
* Set HTTP request header parameters for post or get requests
* @param postandget
* @param headers
* @return
*/
Public Httprequestbase SetHeader (httprequestbase postandget, map<string, string> headers) {
iterator<entry<string, string>> Iterator = Headers.entryset (). Iterator ();
while (Iterator.hasnext ()) {
Entry<string, string> next = Iterator.next ();
Postandget.addheader (Next.getkey (), Next.getvalue ());
}
return postandget;
}

/**
* Gets the response header parameter in the HTTP response
* @param response
* @return
*/
Public map<string, string> GetHeader (closeablehttpresponse response) {
map<string, string> headers = new hashmap<string, string> ();
header[] allheaders = Response.getallheaders ();
for (Header header:allheaders) {
Headers.put (Header.getname (), Header.getvalue ());
}
return headers;
}

/**
* Get native data in HTTP response
* @param entity
* @param consume
* @return
* @throws Exception
*/
Public String getrawcontent (httpentity entity, Boolean consume) throws exception{
String Rawctx = entityutils.tostring (entity);
if (consume) {
Entityutils.consume (entity);
}
return rawctx;
}

/**
* Release HTTP connection
* @param Client
* @throws Exception
*/
public void Clean (Closeablehttpclient client) throws exception{
Client.close ();
}

public static void Main (string[] args) throws Exception {
Httputil httputil = new Httputil ();
/************************************* Create HttpGet request begin ***********************************/
Get an HTTP instance
Closeablehttpclient httpClient = Httputil.gethttpclient ();
Create a HttpGet request
HttpGet HttpGet = Httputil.gethttpget ("http://www.jd.com");
Set HttpGet Request Header parameters
map<string, string> headers = new hashmap<string, string> ();
Headers.put ("User-agent", "mozilla/5.0" (Windows NT 6.1; WOW64; rv:39.0) gecko/20100101 firefox/39.0 ");
Httputil.setheader (httpget, headers);
Submit a HttpGet request while getting an HTTP response
Closeablehttpresponse response = Httpclient.execute (HttpGet);
Get the response header parameters for HTTP
Map<string, string> Header = Httputil.getheader (response);
Gets the native data content in the HTTP response while closing the HTTP underlying connection
String rawcontent = httputil.getrawcontent (Response.getentity (), true);
Release this HTTP request instance
Httputil.clean (httpClient);
/************************************* Create HttpGet request End ***********************************/

HttpPost request the same as the above code, do not demonstrate
}
}

Apache httpclient Component Encapsulation Tool class

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.