Java调用外部接口的方法,最终返回接口response的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
String strURL = "这里是你要调用的接口地址";
URL url = new URL(strURL);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.connect();
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getInputStream()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
httpConn.disconnect();

System.out.println(buffer.toString());

最终这里buffer.toString();就是接口response的值