1.http工具类
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工具类 * * @author qiuwy * */@SuppressWarnings({ "deprecation", "resource" })public class HttpUtil { 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 返回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); }/**
* 根据参数和url访问远程,得到返回值 * * @return */ protected HttpResVo getHttpResult(String url,String method,String parameter) throws Exception { if (StringUtils.isEmpty(method)) { logger.error("method is null."); return null; } // get方法的处理 if (method.equalsIgnoreCase(GET)) { StringBuffer _url = new StringBuffer(url); if (!StringUtils.isEmpty(parameter)) { _url.append("?").append(parameter); System.out.println(_url.toString()); } return HttpUtil.doGet(_url.toString()); } else {// post方法处理 if (!StringUtils.isEmpty(parameter)) { return HttpUtil.doPost(url, parameter); } logger.error("post method has parameter none."); } return null; }}2.http响应返回值
package com.funshion.common.utils;
/** * http响应返回值 * * @author qiuwy * */public class HttpResVo implements java.io.Serializable {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; }}