HttpClient使用方式

本文介绍了一种使用HTTP GET方法从指定URL获取数据的技术,并详细解释了如何在请求中加入Cookies,以及处理重定向和返回数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 发一段核心方法:

 

有些注释,可以看注释。

package org.search.core.http;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.utils.stream.StreamUtils;

/**
 * Get方式获得数据
 * @author Administrator
 *
 */
public class HttpFetcher {
	
	/**
	 * 以GET方式获得数据
	 * @param siteId
	 * @param url
	 * @return
	 */
	public static String fetcher_Get(String siteId, String url){
		try {
			DefaultHttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(url);
			CookieStore cookies = CookiesStore.getInstance().getCookies(siteId);
			if(cookies != null)//添加Cookies
				client.setCookieStore(cookies);
			HttpResponse resp = client.execute(get);
			int status = resp.getStatusLine().getStatusCode();
			/**返回的状态码以30*开头,需要服务器继续跳转*/
			if(status == HttpStatus. SC_MOVED_TEMPORARILY ||
					status == HttpStatus.SC_MOVED_PERMANENTLY||
					status == HttpStatus.SC_SEE_OTHER||
					status == HttpStatus.SC_TEMPORARY_REDIRECT){
				Header[] header = resp.getHeaders("location");
				if(header.length >0){
					String location = header[0].getValue();
					if(location == null || "".equalsIgnoreCase(location)){
						location = "/";
					}
					fetcher_Get(siteId, location);
				}
			}else if(status == HttpStatus.SC_OK){
				//更新Cookies
				CookiesStore.getInstance().storeCookies(siteId, client.getCookieStore());
				return StreamUtils.converStreamToStr(resp.getEntity().getContent(), true);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static void main(String[] args) {
		String content = 
			HttpFetcher.fetcher_Get("baidu", 
					"http://www.baidu.com/s?bs=httpclient+%CF%C2%D4%D8&f=8&wd=httpclient+");
		System.out.println(content);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值