URL url = new URL("http://www.sdfsdfasd.com");
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(30000);
try {
httpURLConnection.connect();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("----");
int responseCode = httpURLConnection.getResponseCode();
System.out.println("responseCode:" + responseCode);
InputStream inputStream = httpURLConnection.getInputStream();
byte[] bytes = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int length = 0;
while((length = inputStream.read(bytes)) != -1){
byteArrayOutputStream.write(bytes,0,length);
}
byte[] byteSum = byteArrayOutputStream.toByteArray();
String urlContent = new String(byteSum);
byteArrayOutputStream.close();
inputStream.close();
System.out.println(urlContent);
详细信息:http://www.cnblogs.com/guodongli/archive/2011/04/05/2005930.html