Return data via http rest request
Copy the code code as follows:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
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.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Tool class for packaging an http request
*
* @author Gu Wei【guwei】 on 14-4-22.3:17 pm
*/
public class HttpClientUtils {
private static final Log log = LogFactory.getLog(HttpClientUtils.class);
/**
* Initialize HttpClient
*/
private static HttpClient httpClient = null;
/**
* Produce HttpClient instance
* Public, static factory method, create the singleton only when needed
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
}
return httpClient;
}
/**
*POST method call
*
* @param url
* @param params The parameter is the NameValuePair key-value pair object
* @return response string
* @throws java.io.UnsupportedEncodingException
*/
public static String executeByPOST(String url, List<NameValuePair> params) {
HttpClient httpclient = getHttpClient();
HttpPost post = new HttpPost(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
if (params != null) {
post.setEntity(new UrlEncodedFormEntity(params));
}
responseJson = httpclient.execute(post, responseHandler);
log.info("HttpClient POST request result: " + responseJson);
} catch (ClientProtocolException e) {
e.printStackTrace();
log.info("HttpClient POST request exception: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().closeExpiredConnections();
httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
}
return responseJson;
}
/**
* Get method request
*
* @param url URL with parameter placeholder, for example: http://****/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}
* @param params parameter value array, which needs to correspond to the order of placeholders in the URL
* @return response string
* @throws java.io.UnsupportedEncodingException
*/
public static String executeByGET(String url, Object[] params) {
HttpClient httpclient = getHttpClient();
String messages = MessageFormat.format(url, params);
HttpGet get = new HttpGet(messages);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
responseJson = httpclient.execute(get, responseHandler);
log.info("HttpClient GET request result: " + responseJson);
} catch (ClientProtocolException e) {
e.printStackTrace();
log.info("HttpClient GET request exception: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.info("HttpClient GET request exception: " + e.getMessage());
} finally {
httpclient.getConnectionManager().closeExpiredConnections();
httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
}
return responseJson;
}
/**
* @param url
* @return
*/
public static String executeByGET(String url) {
HttpClient httpclient = getHttpClient();
HttpGet get = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
responseJson = httpclient.execute(get, responseHandler);
log.info("HttpClient GET request result: " + responseJson);
} catch (ClientProtocolException e) {
e.printStackTrace();
log.info("HttpClient GET request exception: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.info("HttpClient GET request exception: " + e.getMessage());
} finally {
httpclient.getConnectionManager().closeExpiredConnections();
httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
}
return responseJson;
}
}