使用HttpClient版本 4.2.5
准备一已登录用户的sessionid:
【HttpClient使用SESSIONID模拟用户发送请求】
public static void main(String[] args) {String url = "http://10.10.10.90/urcweb/api/http.do?method=get_im_info";
Map header = new HashMap();
header.put("Cookie", "JSESSIONID=C1A34A2EC4C9423BB460E6F7005CA81E");
System.out.println(httpGet(url,null,header));
} /**
* 发送 get 请求
* @param url
* @param encode
* @param headers
* @return
*/
public static String httpGet(String url,String encode,Map headers){
if(encode == null){
encode = "utf-8";
}
String content = null;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
//设置 header
Header headerss[] = buildHeader(headers);
if(headerss != null && headerss.length > 0){
httpGet.setHeaders(headerss);
}
HttpResponse http_response;
try {
http_response = httpclient.execute(httpGet);
HttpEntity entity = http_response.getEntity();
content = EntityUtils.toString(entity, encode);
} catch (Exception e) {
e.printStackTrace();
}finally {
httpGet.releaseConnection();
}
return content;
}
/**
* 组装请求头
* @param params
* @return
*/
public static Header[] buildHeader(Map params){
Header[] headers = null;
if(params != null && params.size() > 0){
headers = new BasicHeader[params.size()];
int i= 0;
for (Map.Entry entry:params.entrySet()) {
headers[i] = new BasicHeader(entry.getKey(),entry.getValue());
i++;
}
}
return headers;
}
转 http://huangqiqing123.iteye.com/blog/2054490
推荐阅读
- starteam|starteam的eclipse插件
- 3D光固化打印机|chitubox micromake L3+ 切片软件配置对应关系
- 选择年月日的插件
- Java 实体类自动生成get set(IDEA)
- 移动端富文本编辑器
- 如何使用axios获取数据
- 我的博客|Vue.js2.0 入门实例(三)(数据对接)
- node|node插件rimraf
- swiper.js插件实现图片滚动效果