import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Test {
public static void main(String[] args) throws IOException {
URL url;
url = new URL("http://www.hao123.com");
URLConnection uc = url.openConnection();
InputStream in = uc.getInputStream();
byte[] ch=new byte[1024];
StringBuilder sb=new StringBuilder();
int c=0;
while ((c = in.read(ch)) != -1){
System.out.println(new String(ch,0,c));
}
in.close();
}
}
+++++++++++封装了一个工具类++++++++++++++++++
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class GetByURL {
private String url;
public GetByURL(String url) {
this.url=url;
}
public String getContents() throws IOException{
URL url2=new URL(this.url);
URLConnection uc = url2.openConnection();
InputStream in = uc.getInputStream();
byte[] ch=new byte[1024];
StringBuilder sb=new StringBuilder();
int c=0;
while ((c = in.read(ch)) != -1){
String str=new String(ch,0,c);
sb.append(str);
}
in.close();
return sb.toString();
}
}