读取网站源码~

前些天无聊...参考网上弄的
package com.url;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlTest{
	public static String getContent(String strUrl){
		try{
			URL url = new URL(strUrl);
			BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
			String s ="";
			StringBuffer sb = new StringBuffer("");
			while((s = br.readLine()) != null){
				sb.append(s+"\r\n");
			}	
			br.close();
			
			return sb.toString();
		}catch (Exception e) {
			return "error open url:"+strUrl;
		}
	}
	
	
	public static void main(String[] args) throws IOException {
		String url = "http://web.qq.com/main.shtml?direct__2#";

		URL server = new URL(url);

		HttpURLConnection connection = (HttpURLConnection)server.openConnection();
		connection.connect();
		System.out.println(getContent(url));
		connection.disconnect();
	}
}

以后的深入研究一下~

你可能感兴趣的:(java,.net,Web,qq)