public static byte[] sendGet(String url, String param) {
InputStream is = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
String urlNameString = url + “?” + param;
URL realUrl = new URL(urlNameString);
System.out.println(“请求路径:”+realUrl);
// 打开和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> map = connection.getHeaderFields();
// 遍历所有的响应头字段
/for (String key : map.keySet()) {
System.out.println(key + “—>” + map.get(key));
}/
// 定义 BufferedReader输入流来读取URL的响应
is = connection.getInputStream();
int length;
byte[] buffer = new byte[4096];
baos = new ByteArrayOutputStream();
while ((length = is.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
bytes = baos.toByteArray();
} catch (Exception e) {
System.out.println(“发送GET请求出现异常!” + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (is != null) {
is.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return bytes;
}
get请求路径
最新推荐文章于 2025-07-10 21:42:50 发布

180

被折叠的 条评论
为什么被折叠?



