import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import sun.net.www.protocol.http.HttpURLConnection;
/**
* 通过加载给定的URL获得页面源文件
* @author HaiCheng
*
*/
public class UrlTest {
public static void main(String[] args) throws IOException {
String str="http://blog.csdn.net/caohaicheng" ;
URL url=new URL(str);
URLConnection con=url.openConnection();
HttpURLConnection connection = (HttpURLConnection)con;//强转成URLConnection的子类 该子类是针对http的请求连接
//设置请求头
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; N; Windows NT 5.1; zh-CN) AppleWebKit/533.3 (KHTML, like Gecko) Qt/4.7.1 Safari/533.3");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.connect();
System.out.println( connection.getResponseCode());//HTTP响应码 200代表请求成功
BufferedReader reader = null;
InputStream is = null;
String currentLine = "";
String content = "";
is = connection.getInputStream();//URLConnection的方法 返回写入到此连接的输出流
reader = new BufferedReader(new InputStreamReader(is , "UTF-8"));
while((currentLine = reader.readLine()) != null)
{
if(currentLine.length()>0)
{
content = content + currentLine.trim();
}
}
System.out.println(content);
//将content内容放到EditPlus就是浏览器请求str得到的网页
}
}
加载URL获得页面源代码
最新推荐文章于 2021-06-20 23:17:55 发布