package com.java.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
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.HttpClients;
import org.apache.http.util.EntityUtils;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author jiangli
* @date 2018/5/22 9:59
* 发送post和get请求
* 返回值为响应的json字符串
*/
public class HttpClientUtil {
/**
* @param url 发送请求的 URL
* @param postData 需要post的数据
* @param headers 请求头
* @return 所代表远程资源的响应结果
*/
public static String dopost(String url, String postData, Map<String, String> headers) {
HttpPost post = new HttpPost();
HttpResponse response = null;
try {
post.setURI(new URI(url));
if (StringUtils.isNotBlank(postData)) {
post.setEntity(new StringEntity(postData, "UTF-8"));
}
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
post.addHeader(entry.getKey(), entry.getValue());
}
}
HttpClient httpClient = HttpClients.createDefault();
response = httpClient.execute(post);
return EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
throw new RuntimeException("got an error from HTTP for url:" + url, e);
} finally {
if (response != null) {
EntityUtils.consumeQuietly(response.getEntity());
}
post.releaseConnection();
}
}
public static String doget(String url, Map<String, String> headers) {
HttpGet get = new HttpGet();
HttpResponse response = null;
try {
get.setURI(new URI(url));
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
get.addHeader(entry.getKey(), entry.getValue());
}
}
HttpClient httpClient = HttpClients.createDefault();
response = httpClient.execute(get);
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new RuntimeException("got an error from HTTP for url:" + url, e);
} finally {
if (response != null) {
EntityUtils.consumeQuietly(response.getEntity());
}
get.releaseConnection();
}
}
public static void main(String[] args) {
// 请求头
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("accept", "application/json; charset=utf-8");
// 进行http的post请求
JSONObject js = JSONObject.parseObject(dopost(
"https://test-api.pingan.com.cn:20443/oauth/oauth2/access_token?client_id=P_OKAY001&grant_type=client_credentials&client_secret=VuVEh743",
null, headers));
String token = ((JSONObject) js.get("data")).getString("access_token");
//System.out.println(token);
String aeskey = "802CE4BBD34F1B65";
JSONObject jo = new JSONObject();
jo.put("couponNo", "66666666");
jo.put("partnerOrderId", "777777");
jo.put("outletId", "9999999");
String data = jo.toString();
data = AESUtils.encrypt(data, aeskey);
System.err.println(data);
String req = "https://test-api.pingan.com.cn:20443/open/appsvr/property/partner/redemption?access_token=" + token + "&request_id=P_HQMC999&userName=13788888305";
JSONObject jsonObject = new JSONObject();
jsonObject.put("data",
data);
jsonObject.put("partnerId", "P_HUANQIUMINGCHE");
jsonObject.put("timestamp", new Date().getTime());
// 生成需要post的json字符串
String postData = jsonObject.toJSONString();
String resp = dopost(req, postData, headers);
System.out.println(resp);
// JSONObject po = JSONObject.parseObject(resp);
// String string = ((JSONObject) po.get("data")).getString("message");
// System.out.println("message is "+string);
}
}
补充:上面Main方法用Restlet Client模拟后的返回如下
如何取出data中的message:
JSONObject po = JSONObject.parseObject(resp);
String string = ((JSONObject) po.get("data")).getString("message");
System.out.println("message is "+string);