1.http Tool Class
Package com.funshion.common.utils;
Import Java.net.URI;
Import Java.net.URL;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.protocol.HTTP;
Import Org.apache.http.util.EntityUtils;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
/**
* HTTP Tool class
*
* @author Qiuwy
*
*/
@SuppressWarnings ({"Deprecation", "resource"})
public class Httputil {
public static final int http_code_200 = 200;
private static Logger Logger = Loggerfactory.getlogger (Httputil.class);
/**
* Get
*
* @param URL
* @return
* @throws Exception
*/
public static HTTPRESVO doget (String getUrl) throws Exception {
URL url = new URL (getUrl);
Uri uri = new Uri (Url.getprotocol (), NULL, Url.gethost (), Url.getport (), Url.getpath (), url.getquery (), NULL);
HttpClient HttpClient = new Defaulthttpclient ();
HttpGet httpget = new HttpGet (URI);
HttpResponse response = Httpclient.execute (HttpGet);
int httpcode = Response.getstatusline (). Getstatuscode ();
String result = entityutils.tostring (response.getentity ());
Logger.info ("get.url=" + URI + ", response code=" + Httpcode + ", response entity=" + result);
return new HTTPRESVO (Httpcode, result);
}
/**
* Post
*
* @param URL
* @param params
* @return return HTTP Code
*
*/
public static HTTPRESVO doPost (string url, string params) throws Exception {
URL posturl = new URL (URL);
Uri uri = new Uri (Posturl.getprotocol (), NULL, Posturl.gethost (), Posturl.getport (), Posturl.getpath (), Posturl.getquery (), NULL);
HttpClient HttpClient = new Defaulthttpclient ();
HttpPost HttpPost = new HttpPost (URI);
stringentity entity = new Stringentity (params, HTTP. UTF_8);
Entity.setcontenttype ("Application/json;charset=utf-8");
Httppost.setheader ("Accept", "Application/json");
Httppost.setentity (entity);
HttpResponse response = Httpclient.execute (HttpPost);
int httpcode = Response.getstatusline (). Getstatuscode ();
String result = entityutils.tostring (response.getentity ());
Logger.info ("post.url=" + URI + ", response code=" + Httpcode + ", response entity=" + result);
return new HTTPRESVO (Httpcode, result);
}
/**
* Remote access based on parameter and URL, get return value
*
* @return
*/
Protected Httpresvo Gethttpresult () throws Exception {
if (Stringutils.isempty (This.method)) {
Logger.error ("method is null.");
return null;
}
Handling of Get methods
if (This.method.equalsIgnoreCase (GET)) {
StringBuffer _url = new StringBuffer (This.url);
if (! Stringutils.isempty (This.parameter)) {
_url.append ("?"). Append (This.parameter);
System.out.println (_url.tostring ());
}
Return Httputil.doget (_url.tostring ());
} else {//POST method processing
if (! Stringutils.isempty (This.parameter)) {
Return Httputil.dopost (This.url, This.parameter);
}
Logger.error ("POST method has parameter none.");
}
return null;
}
}
2.http Response return value
Package com.funshion.common.utils;
/**
* HTTP Response return value
*
* @author Qiuwy
*
*/
public class Httpresvo implements Java.io.Serializable {
public static final int STATUS_OK = 200;
Private static final long serialversionuid = -3063948675189138529l;
private int httpcode;
Private String result;
Public Httpresvo () {
}
Public httpresvo (int httpcode, String result) {
This.httpcode = Httpcode;
This.result = result;
}
public int Gethttpcode () {
return httpcode;
}
public void Sethttpcode (int httpcode) {
This.httpcode = Httpcode;
}
Public String GetResult () {
return result;
}
public void Setresult (String result) {
This.result = result;
}
}
Java HTTP GET and POST requests