时间:2021-05-19
通过http rest请求返回数据
复制代码 代码如下:
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;
/**
* 分装一个http请求的工具类
*
* @author 顾炜【guwei】 on 14-4-22.下午3:17
*/
public class HttpClientUtils {
private static final Log log = LogFactory.getLog(HttpClientUtils.class);
/**
* 初始化HttpClient
*/
private static HttpClient httpClient = null;
/**
* 生产HttpClient实例
* 公开,静态的工厂方法,需要使用时才去创建该单体
*
* @return
*/
public static HttpClient getHttpClient() {
if (httpClient == null) {
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
}
return httpClient;
}
/**
* POST方式调用
*
* @param url
* @param params 参数为NameValuePair键值对对象
* @return 响应字符串
* @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请求结果:" + responseJson);
} catch (ClientProtocolException e) {
e.printStackTrace();
log.info("HttpClient POST请求异常:" + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().closeExpiredConnections();
httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
}
return responseJson;
}
/**
* Get方式请求
*
* @param url 带参数占位符的URL,例:http://****/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}
* @param params 参数值数组,需要与url中占位符顺序对应
* @return 响应字符串
* @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请求结果:" + responseJson);
} catch (ClientProtocolException e) {
e.printStackTrace();
log.info("HttpClient GET请求异常:" + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.info("HttpClient GET请求异常:" + 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请求结果:" + responseJson);
} catch (ClientProtocolException e) {
e.printStackTrace();
log.info("HttpClient GET请求异常:" + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.info("HttpClient GET请求异常:" + e.getMessage());
} finally {
httpclient.getConnectionManager().closeExpiredConnections();
httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
}
return responseJson;
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Java实现Http工具类的封装操作。分享给大家供大家参考,具体如下:http工具类的实现:(通过apache包)第一个类importjava.i
如何通过Java发送HTTP请求,通俗点讲,如何通过Java(模拟浏览器)发送HTTP请求。Java有原生的API可用于发送HTTP请求,即java.net.U
一、前言如何通过Java发送HTTP请求,通俗点讲,如何通过Java(模拟浏览器)发送HTTP请求。Java有原生的API可用于发送HTTP请求,即java.n
除了服务器类,还包括请求类和响应类请求类:获取客户的HTTP请求,分析客户所需要的文件响应类:获得用户请求后将用户需要的文件读出,添加上HTTP应答头。发送给客
本文实例为大家分享了基于C#实现网页爬虫的详细代码,供大家参考,具体内容如下HTTP请求工具类:功能:1、获取网页html2、下载网络图片usingSystem