1、实例化URL对象
URL url = new URL("https://www.baidu.com/s?ie=UTF-8&wd=get");
2、通过URL对象打开链接获取HttpURLConnection的实例化对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3、设置请求方式
conn.setRequestMethod("GET");
4、通过HttpURLConnection对象获取输入流
InputStream is = conn.getInputStream();
5、将输入流转换成String类型
//把一个inputStream 转换成一个String
public String inputStreamToString(InputStream is) throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while((len=is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
String content = new String(baos.toByteArray());
return content;
}
String result = inputStreamToString(is);
或者使用BufferReader类转换
BufferedReader r