/**
* @Author liheping
* @Description //发送get请求携带token
* @Date 2021/12/30 14:25
**/
public static String httpGet(String url,String token) throws URISyntaxException {
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse ;
String finalString = null;
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("accept", "*/*");
httpGet.setHeader("Accept-Encoding","gzip, deflate");
httpGet.setHeader("Cache-Control","no-cache");
httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
httpGet.setHeader("authorization",token);
System.out.println("发起请求的信息:"+httpGet);
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
finalString= EntityUtils.toString(entity, "UTF-8");
try {
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return finalString;
}
java发送get请求
最新推荐文章于 2024-08-31 14:09:04 发布