-- 作者:admin
-- 发布时间:2011/6/25 11:06:18
-- 基于Apache的HTTPClient的网页获取方法
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader;
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient;
public class Exec { public static void main(String[] args) { HttpClient httpclient = new DefaultHttpClient(); String startUrl = httpwww.baidu.com; try { HttpGet httpget = new HttpGet(startUrl); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity == null) { System.out.println(Error + httpget.getURI()); } else { InputStream is = entity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader( is, UTF-8)); try { StringBuffer sb = new StringBuffer(); String str = br.readLine(); while (str != null) { sb.append(str + (char) 13 + (char) 10); str = br.readLine(); } System.out.println(sb); } catch (Exception e) { httpget.abort(); } finally { try { is.close(); } catch (Exception ignore) { } } System.out.println(Success + httpget.getURI()); } } catch (Exception e) { } finally { httpclient.getConnectionManager().shutdown(); } } }
相关的所需包:http://hc.apache.org/
|