java后台HTTP请求其他项目接口

需要引用的包

org.apache.httpcomponents httpclient 4.3.5

创建一个工具类对项目以外的接口惊喜HTTP请求,该接口返回的是一个String的字符串,这个字符串就是你需要的数据,在需要的地方调用该接口

static public String get(String url) throws IOException{
//创建一个默认连接
String result=null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
//获取状态吗,判断状态
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode==200){
org.apache.http.HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity,“UTF-8”);
System.out.println(“result:”+result);
}
return result;
}

你可能感兴趣的:(java后台)