Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共1 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:基于Apache的HTTPClient的网页获取方法

1楼
admin 发表于:2011/6/25 11:06:18

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/

 

共1 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .01563 s, 2 queries.