HttpClient使用方式

 发一段核心方法:

 

有些注释,可以看注释。

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);
	}
}

 

你可能感兴趣的:(apache,F#)