an HTTP Request tool class used in the work
Package com.qiantu.core.utils;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.security.KeyStore;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.NameValuePair;
Import org.apache.http.client.ClientProtocolException;
Import org.apache.http.client.HttpClient;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpPost;
Import Org.apache.http.conn.scheme.Scheme;
Import Org.apache.http.conn.ssl.SSLSocketFactory;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.util.EntityUtils;
Import Com.alibaba.fastjson.JSON; /** * @date 2015-05-31 Morning 9:56:10 * @author xuning * * */public class Httputil {private static final String default_encoding = "UTF-8";
/** * Get mode request * * @param URI * Server URI to use physical IP or domain name, do not recognize localhost or 127.0.0.1 form! * @return * @throws IOException * @throws clientprotocolexception/public static String get (String uri) throws Cl
Ientprotocolexception, IOException {httpget httpget = new HttpGet (URI);
HttpClient httpclient = new Defaulthttpclient ();
HttpResponse HttpResponse = Httpclient.execute (HttpGet);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode () = =) {String result = entityutils.tostring (httpresp
Onse.getentity ());
Return entityutils.tostring (Httpresponse.getentity ());
} throw new IOException ("status is" + StatusCode);
/** * Get mode request * * @param URI * Server URI to use physical IP or domain name, do not recognize localhost or 127.0.0.1 form! * @return * @throws IOException * @throws clientprotocolexception/public static string get (string uri, map<st Ring, string> Parammap) throws Clientprotocolexception, Ioexception {StringBuilder sb = new StringBuilder (URI);
if (Parammap!= null) {Boolean isbegin = true; For (String Key:paramMap.keySet ()) {if (isbegin) {sb.append ("?").
Append (Key). Append ("="). Append (Parammap.get (key));
Isbegin = false;
else {sb.append ("&"). Append (Key). Append ("="). Append (Parammap.get (key));
}} httpget HttpGet = new HttpGet (sb.tostring ());
HttpClient httpclient = new Defaulthttpclient ();
HttpResponse HttpResponse = Httpclient.execute (HttpGet);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = () {//String result = entityutils.tostring (httpre
Sponse.getentity ());
Return entityutils.tostring (Httpresponse.getentity ());
} throw new IOException ("status is" + StatusCode);
/** * Get mode request HTTPS * * @param URI * Server URI to use physical IP or domain name, do not recognize localhost or 127.0.0.1 form! * @return * @throws IOException * @throws clientprotocolexception */public static string Httpsget (String uri, String keyfile, String keypwd) throws Exception {HttpGet HttpGet = new
HttpGet (URI);
HttpClient httpclient = newhttpsclient (KeyFile, keypwd);
HttpResponse HttpResponse = Httpclient.execute (HttpGet);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = () {//String result = entityutils.tostring (httpre
Sponse.getentity (),//default_encoding);
Return entityutils.tostring (Httpresponse.getentity (), default_encoding);
} throw new IOException ("status is" + StatusCode);
/** * Post Mode request * * @param URI * Server URI to use physical IP or domain name, do not recognize localhost or 127.0.0.1 form! * @param parammap * @return * @throws IOException * @throws clientprotocolexception/public static String post ( String URI, map<string, string> parammap) throws Clientprotocolexception, IOException {httppost httppost = new
HttpPost (URI); list<namevaluepair> params = new Arraylist<nameValuepair> (); if (Parammap!= null) {for (String Key:paramMap.keySet ()) {Params.add (New Basicnamevaluepair (Key, Parammap.get (
Key));
} httppost.setentity (New urlencodedformentity (params, "utf-8"));
} HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httppost);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = = () {return entityutils.tostring (httpresponse.get
Entity (), default_encoding);
} throw new IOException ("status is" + StatusCode);
/** * Post mode request, UTF-8 encoded send content * * @param uri * @param parammap * @return * @throws clientprotocolexception * @throws IOException */public static string postencoding (String uri, map<string, string> parammap) throws C
Lientprotocolexception, IOException {httppost httppost = new HttpPost (URI);
list<namevaluepair> params = new arraylist<namevaluepair> (); if (Parammap!= null) {for (String Key:paramMap.keySet ()) {pArams.add (New Basicnamevaluepair (Key, Parammap.get (key));
} httppost.setentity (New urlencodedformentity (params, default_encoding));
} HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httppost);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = = () {return entityutils.tostring (httpresponse.get
Entity (), default_encoding);
} throw new IOException ("status is" + StatusCode);
/** * Post Mode request * * @param URI * Server URI to use physical IP or domain name, do not recognize localhost or 127.0.0.1 form! * @param parammap * @param headers * @return * @throws clientprotocolexception * @throws ioexception/Public static string post (string Uri, map<string, string> Parammap, map<string, string> headers) throws Clientprot
Ocolexception, IOException {httppost httppost = new HttpPost (URI);
if (headers!= null) {for (String Key:headers.keySet ()) {Httppost.setheader (key, Headers.get (key)); }} list&Lt
namevaluepair> params = new arraylist<namevaluepair> (); if (Parammap!= null) {for (String Key:paramMap.keySet ()) {Params.add (New Basicnamevaluepair (Key, Parammap.get (
Key));
} httppost.setentity (New urlencodedformentity (params, default_encoding));
} HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httppost);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = = () {return entityutils.tostring (httpresponse.get
Entity (), default_encoding);
} throw new IOException ("status is" + StatusCode);
/** * Post Mode request HTTPS * * @param URI * Server URI to use physical IP or domain name, do not recognize localhost or 127.0.0.1 form! * @param parammap * @return * @throws IOException * @throws clientprotocolexception/public static String HTTPS
Post (String uri, map<string, string> parammap, String keyfile, String keypwd) throws Clientprotocolexception, IOException, Exception {httppost httppost = new HTTPPOST (URI);
list<namevaluepair> params = new arraylist<namevaluepair> (); if (Parammap!= null) {for (String Key:paramMap.keySet ()) {Params.add (New Basicnamevaluepair (Key, Parammap.get (
Key));
} httppost.setentity (New urlencodedformentity (params, default_encoding));
} HttpResponse HttpResponse = Newhttpsclient (KeyFile, Keypwd). Execute (httppost);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = = () {return entityutils.tostring (httpresponse.get
Entity ());
} throw new IOException ("status is" + StatusCode); * * * NEW httpsclient/private static httpclient newhttpsclient (String keyfile, String keypwd) throws Exception
{KeyStore Truststore = keystore.getinstance ("BKS");
Truststore.load (New FileInputStream (New File (keyfile)), Keypwd.tochararray ());
Sslsocketfactory socketfactory = new Sslsocketfactory (Truststore);
Scheme sch = new scheme ("https", Socketfactory, 8443); HttpclieNT client = new Defaulthttpclient ();
Client.getconnectionmanager (). Getschemeregistry (). Register (Sch);
return client; /** * POST request send transmit obj object * * @param uri * @param parammap * @return * @throws clientprotocolexception * @t Hrows IOException */public static String Postofobject (String uri, Object obj) throws Clientprotocolexception, Ioexc
eption {String params = json.tojsonstring (obj);
Return Httputil.postjson (URI, params); /** * For HTTP transmission JSON data processing * * @param URI * @param parameters * @return * @throws clientprotocolexception * @throws IOException */public static string Postjson (string uri, string parameters) throws Clientprotocolexception,
IOException {HttpPost HttpPost = new HttpPost (URI);
if (parameters!= null) {stringentity entity = new Stringentity (parameters);
Entity.setcontentencoding ("UTF-8");
Entity.setcontenttype ("Application/json");
Httppost.setentity (entity); } HttpResponse HttpResponse = new DefAulthttpclient (). Execute (httppost);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = = () {return entityutils.tostring (httpresponse.get
Entity (), default_encoding);
} throw new IOException ("status is" + StatusCode); /** * For HTTP transmission JSON data processing * * @param URI * @param parameters * @return * @throws clientprotocolexception * @throws IOException */public static string Postbyjson (string uri, string parameters) throws Clientprotocolexceptio
N, ioexception {httppost httppost = new HttpPost (URI);
if (parameters!= null) {stringentity entity = new Stringentity (Parameters, "UTF-8");
Entity.setcontentencoding ("UTF-8");
Entity.setcontenttype ("Application/json");
Httppost.setentity (entity);
} HttpResponse HttpResponse = new Defaulthttpclient (). Execute (httppost);
int StatusCode; if ((StatusCode = Httpresponse.getstatusline (). Getstatuscode ()) = = () {return entityutils.tostring (httpresponse.get EntitY (), default_encoding);
} throw new IOException ("status is" + StatusCode); }
}