前些天无聊...参考网上弄的
以后的深入研究一下~
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(); } }
以后的深入研究一下~