版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LQM1991/article/details/78298899
近期收到任务需要封装下别的开发组写的接口以实现权限控制,于是想起了httpclient,直接上代码吧
@Test
public void post() {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// CookieStore cookieStore = new BasicCookieStore();
// cookieStore.addCookie();
// CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore ).build();
// 创建httppost
// HttpPost httppost = new HttpPost("http://localhost:8080/api/v2/bi/report/getPersonas");
HttpPost httppost = new HttpPost("http://10.0.4.62:8000/api/v1/car_income");
// HttpPost httppost = new HttpPost("http://localhost:8080/api/v1/user/login");
try {
JSONObject jsonParam = new JSONObject();
// jsonParam.put("staffEname", "zeezhang");
// jsonParam.put("oaPwd", "7c4a8d09ca3762af61e59520943dc26494f8941b");
// jsonParam.put("statDim", "gender");
// jsonParam.put("groupby","all");
// jsonParam.put("filters","{}");
// jsonParam.put("start_date","2017-10-01");
// jsonParam.put("end_date","2017-10-17");
// jsonParam.put("dimensions","['company_code','last_expired_time']");
StringEntity entity = new StringEntity("{\"groupby\":\"all\",\"start_date\":\"2017-01-01\",\"end_date\":\"2017-10-19\",\"dimensions\":[],\"filters\":{}}", "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
// httppost.addHeader("Cookie", "JSESSIONID=0d9fc7de-19ab-4a8e-85b4-fe3b5526a4c4");
httppost.addHeader("Authorization", "Basic ZG1hcGlfdGVzdDpkbUAyMDE3MDkwNQ==");
httppost.setEntity(entity);
System.out.println("executing request:==== " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
// String value = response.getFirstHeader("Set-Cookie").getValue();
// System.out.println(value+"====================");
try {
HttpEntity entity1 = response.getEntity();
if (entity1 != null) {
System.out.println("--------------------------------------");
System.out.println(entity1);
System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity1, "UTF-8"));
System.out.println("--------------------------------------");
}
} finally {
response.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();
}
}
}
上边是json字符串格式的请求参数针对post请求
/**
* post方式提交表单(模拟用户登录请求)
*/
@Test
public void postForm() {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost("http://localhost:8080/api/v1/user/login");
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("oaPwd", "7c4a8d09ca3762af61e59520943dc26494f8941b"));
formparams.add(new BasicNameValuePair("staffEname", "aaa"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
System.out.println("--------------------------------------");
}
} finally {
response.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();
}
}
}
上边是针对表单提交的post方式,get方式的就不再多说了