package text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.MalformedInputException;
public class GetHtml {
/**java项目源www.fhadmin.org
* @param url 网址
* @param encoding 编码
* @return 返回string
*/
public static String getHtmlResouceUrl(String url,String encoding){
StringBuffer buffer= new StringBuffer();
URL urlObj=null;
URLConnection uc=null;
InputStreamReader isr=null;
BufferedReader reader=null;
String line=null;
try {
urlObj=new URL(url);
uc = urlObj.openConnection();
isr=new InputStreamReader(uc.getInputStream(), encoding);
reader=new BufferedReader(isr);
while((line=reader.readLine())!=null){
buffer.append(line+"\n");
}
}catch (MalformedInputException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("网络打开失败");
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("网络连接失败");
}finally{
if(isr!=null){
try {
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return buffer.toString();
}
public static void main(String[] args) {
String url="http://www.1b23.com";
String econding="utf-8";
String html=getHtmlResouceUrl(url,econding);
System.out.println(html);
}
}