58,454
社区成员
发帖
与我相关
我的任务
分享
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
{
"status": 0,
"message": "query ok",
"count": 10,
"data": [
{
"id": "14510915388143376029",
"title": "武汉欢乐谷",
"address": "湖北省武汉市洪山区欢乐大道196号",
"type": 0,
"location": {
"lat": 30.59498,
"lng": 114.39495
},
"adcode": 420111,
"province": "湖北省",
"city": "武汉市",
"district": "洪山区"
},
{
"id": "17016117849526861614",
"title": "武汉职业技术学院(东区)",
"address": "湖北省武汉市洪山区关山大道463号",
"type": 0,
"location": {
"lat": 30.48404,
"lng": 114.41519
},
"adcode": 420111,
"province": "湖北省",
"city": "武汉市",
"district": "洪山区"
},
{
"id": "1728086002144287451",
"title": "武汉空中健身(悦荟店)",
"address": "湖北省武汉市江岸区江汉路",
"type": 0,
"location": {
"lat": 30.58368,
"lng": 114.28798
},
"adcode": 420102,
"province": "湖北省",
"city": "武汉市",
"district": "江岸区"
}
],
"request_id": "687527276294144605"
}
"{ "status": 0, "message": "query ok", "count": 0, "data": [], "request_id": "12692708388900200275"}"