话不多说
http请求代码
public String doGet(String url,Map<String,String> map){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String userNo = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
map.entrySet().stream()
.forEach(entrySet -> uriBuilder.setParameter(entrySet.getKey(), entrySet.getValue()));
HttpGet httpget = new HttpGet(uriBuilder.build());
//构造请求头部参数,如果没有头部参数可以不需要
httpget.setHeader("token",map.get("token"));
//创建http请求
httpClient = HttpClients.createDefault();
response = httpClient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jsonObject = JSONObject.parseObject(result);
//返回值是一个json,然后将json字符串进行转化获取想要的信息。
String data = JSONObject.toJSONString(jsonObject.get("data"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (httpClient != null) {
try {
//关闭请求
httpClient.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
Https请求代码
public String doGet(String url,Map<String,String> map){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String userNo = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
map.entrySet().stream()
.forEach(entrySet -> uriBuilder.setParameter(entrySet.getKey(), entrySet.getValue()));
HttpGet httpget = new HttpGet(uriBuilder.build());
httpget.setHeader("token",map.get("token"));
//创建一个Htpps请求
httpClient = SSLClient.wrapClient();
response = httpClient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jsonObject = JSONObject.parseObject(result);
String data = JSONObject.toJSONString(jsonObject.get("data"));
JSONObject jsonObject1 = JSONObject.parseObject(data);
userNo = JSONObject.toJSONString(jsonObject1.get("userNo"));
userNo = userNo.replace("\"","");
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
return userNo ;
}
绕过证书 创建SSLClient
class SSLClient {
/**
* 绕过证书
* @return 返回构造完成的client
*/
public static CloseableHttpClient wrapClient() {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(
ctx, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(ssf).build();
return httpclient;
} catch (Exception e) {
return HttpClients.createDefault();
}
}