JAVA HttpURLConnection 获取网页内容

JAVA HttpURLConnection 获取网页内容



import java.net.URL;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

class Hello{
private String name;
private static HttpURLConnection connection = null;

public static String httpRequest(String url){
String content = "";
try{
URL u = new URL(url);
connection = (HttpURLConnection)u.openConnection();
connection.setRequestMethod("GET");
int code = connection.getResponseCode();
if(code == 200){
InputStream in = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(in,"utf-8");
BufferedReader reader = new BufferedReader(isr);
String line = null;
while((line = reader.readLine()) != null){
content += line;
}
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(connection != null){
connection.disconnect();
}
}
return content;
}

public static void main(String[] args){
String content = httpRequest("http://www.baidu.com/");
System.out.println(content);
}
}

你可能感兴趣的:(Java)