主函数代码块:
HttpClient httpClient = HttpClientBuilder.create().build();
// Get请求
URIBuilder uriBuilder = new URIBuilder(payurl);
if (StringUtils.isNotEmpty(reqJson)) {
List<NameValuePair> nameValuePairList = json2NameValuePairList(JSON.parseObject(reqJson));
uriBuilder.setParameters(nameValuePairList);
}
HttpGet httpGet = new HttpGet(uriBuilder.build());
// 设置Header
httpGet.setHeader("Content-Type", "application/json");
HttpResponse httpResponse = httpClient.execute(httpGet);
公共方法块:
private List<NameValuePair> json2NameValuePairList(JSONObject params) {
if (params != null && !params.isEmpty()) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (entry.getValue() != null) {
String value = String.valueOf(entry.getValue());
list.add(new BasicNameValuePair(entry.getKey(), value));
}
}
return list;
}
return null;
}