发送http请求并接收返回数据

本文介绍了一个使用Java编写的公共静态方法,该方法通过URL字符串获取并返回整个网页的内容。此方法首先创建URL对象,然后打开与该URL的连接,并使用BufferedReader逐行读取内容,最终将所有读取的数据整合成一个String对象返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static String getDocumentAt(String urlString) {
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(urlString);// 生成url对象
URLConnection urlConnection = url.openConnection();// 打开url连接
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (MalformedURLException e) {
System.out.println("不能连接到URL:" + urlString);
e.printStackTrace();
} catch (IOException e) {
System.out.println("不能连接到URL:" + urlString);
e.printStackTrace();
}
return sb.toString();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值