httpclient使用实践

httpclient是什么这里不再详述(可参考最下方网址);直接上代码;接口返回json格式的数据。

package com.httpclient.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ThttpClient {

public static void main(String[] args) {
String baseURL = "http://192.168.17.75:8080";
String token = "66b5c33d-4d6b-49cd-bb54-303b6d0780b5";

CloseableHttpClient httpclient = HttpClients.createDefault();
createFaciGraphSession(httpclient, baseURL, token);
httpclient = HttpClients.createDefault();
String result = (String) getBlendingPlanList(httpclient, baseURL, token, 1L);
System.out.println(result);
}


private static void createFaciGraphSession(CloseableHttpClient httpclient, String baseURL, String token) {
// 创建默认的httpClient实例.
// CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(baseURL + "/system/sessionManage/createSession?_token="+token);
// 执行get请求.

try {
CloseableHttpResponse res = httpclient.execute(httpget);
// CloseableHttpResponse res = httpclient.execute(httppost);
try {
HttpEntity entity = res.getEntity();
if (entity != null) {
System.out.println("----------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
System.out.println("--------------------------------");
}
} finally {
res.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static Object getBlendingPlanList(CloseableHttpClient httpclient, String baseURL, String token, Long planId){
String result = null;
// HttpGet httpget = new HttpGet(baseURL + "/third/planInfo/getBlendingPlanIds?_token="+token+"&planId=" + planId);
HttpGet httpget = new HttpGet(baseURL + "/third/planInfo/getBlendingPlanList?_token="+token+"&planId=" + planId);
// 执行get请求.
try {
CloseableHttpResponse res = httpclient.execute(httpget);
// CloseableHttpResponse res = httpclient.execute(httppost);
try {
HttpEntity entity = res.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
} finally {
res.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}


参考自:http://blog.csdn.net/wangpeng047/article/details/19624529
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值