postman请求:
请求详情:
请求地址:https://api.shenjian.io/?appid=205f4451a9d9d71d62c186d609066e89
请求参数:
请求方式:GET/POST
JAVA代码实现:
String appid="205f4451a9d9d71d62c186d609066e89"
String httpUrl = "https://api.shenjian.io/";
String httpArg = "appid="+appid;
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
/**
* @param urlAll
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*/
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.connect();
InputStream is = new GZIPInputStream(connection.getInputStream());
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}