课外天地 李树青学习天地信息检索原理课件 → 基于Apache的HTTPClient的网页获取方法


  共有22681人关注过本帖树形打印复制链接

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

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
基于Apache的HTTPClient的网页获取方法  发帖心情 Post By: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/

 


 回到顶部