发送一个 http --get请求
public String get(String url) {
try {
// Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(
// "127.0.0.1", 10808));
URL thisurl = new URL(url); // 把字符串转换为URL请求地址
// HttpURLConnection connection = (HttpURLConnection) thisurl
// .openConnection(proxy);// 打开连接
HttpURLConnection connection = (HttpURLConnection) thisurl.openConnection();// 打开连接
connection.connect();// 连接会话
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
HTTP请求
最新推荐文章于 2022-10-26 14:58:15 发布