1. 程式人生 > >HttpClient 學習1

HttpClient 學習1

HttpClient不是一個瀏覽器。它是一個客戶端的 HTTP 通訊實現庫。HttpClient 的目標是傳送和接收 HTTP 報文。 HttpClient 不會去快取內容,執行嵌入在 HTML 頁面中

的 javascript 程式碼,猜測內容型別,重新格式化請求/重定向 URI,或者其它和 HTTP運輸無關的功能。

HttpClient最重要的功能是執行HTTP方法。一個HTTP方法的執行,包含一個或多個http請求/http響應。

簡單的請求過程的例項:

package ch1;

import java.io.InputStream;

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.HttpClients;

public class Test1 {

	public static void main(String[] args) throws Exception {
		//建立一個HttpClient;
		HttpClient hc=HttpClients.createDefault();
		//建立一個get請求
		HttpGet httpGet=new HttpGet("http://www.baidu.com");
		//執行請求,返回Response
		HttpResponse httpResponse=hc.execute(httpGet);
		HttpEntity httpEntity=httpResponse.getEntity();
		//獲取內容
		InputStream is=httpEntity.getContent();
		byte[] b=new byte[1024];
		int i;
		StringBuffer sb=new StringBuffer();
		while((i=is.read(b))!=-1){
			sb.append(new String(b));
		}
		is.close();
		System.out.println(sb.toString());
	}
}

使用httpclient提供的方法建立帶引數的uri:

package ch1;

import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class Test2 {

	public static void main(String[] args) throws Exception {
		//http://www.baidu.com/s?tn=baiduhome_pg&ie=utf-8&bs=httpclient+中文+pdf&f=8
		//&rsv_bp=1&wd=csdn&rsv_sug3=5&rsv_sug4=229&rsv_sug1=6&rsv_sug2=0&inputT=4
		
		HttpClient hc=HttpClients.createDefault();
		
		
		//將請求的引數存在這個集合中
		List<NameValuePair> nameValues=new ArrayList<NameValuePair>();
		
		nameValues.add(new BasicNameValuePair("tn", "baiduhome_pg"));
		nameValues.add(new BasicNameValuePair("ie", "utf-8"));
		nameValues.add(new BasicNameValuePair("bs", "httpclient+中文+pdf"));
		nameValues.add(new BasicNameValuePair("f", "8"));
		nameValues.add(new BasicNameValuePair("rsv_bp", "1"));
		nameValues.add(new BasicNameValuePair("wd", "csdn"));
		nameValues.add(new BasicNameValuePair("rsv_sug3", "5"));
		nameValues.add(new BasicNameValuePair("rsv_sug4", "299"));
		nameValues.add(new BasicNameValuePair("rsv_sug1", "6"));
		nameValues.add(new BasicNameValuePair("rsv_sug2", "0"));
		nameValues.add(new BasicNameValuePair("inputT", "4"));
		
		//建立一個URI
		@SuppressWarnings("deprecation")
		URI uri=URIUtils.createURI("http", "www.baidu.com", -1, "/s",URLEncodedUtils.format(nameValues, "utf-8") ,null);
		System.out.println(uri);
		HttpGet httpGet=new HttpGet(uri);
		
		HttpResponse httpResponse=hc.execute(httpGet);
		HttpEntity httpEntity=httpResponse.getEntity();
		//獲取內容
		InputStream is=httpEntity.getContent();
		byte[] b=new byte[1024];
		int i;
		StringBuffer sb=new StringBuffer();
		while((i=is.read(b))!=-1){
			sb.append(new String(b));
		}
		is.close();
		System.out.println(sb.toString());
		
	}
}

Http響應:

package ch1;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.message.BasicHttpResponse;

public class Test3 {

	public static void main(String[] args) {
		
		//模擬建立一個HttpResponse
		HttpResponse httpRes = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus.SC_OK,"OK");
		
		System.out.println(httpRes.getProtocolVersion());
		System.out.println(httpRes.getStatusLine());
	}
}

獲取返回資訊的頭資料:
//執行請求,返回Response
		HttpResponse httpResponse=hc.execute(httpGet);
		//獲取狀態
		System.out.println(httpResponse.getStatusLine());
		//獲取頭資訊
		System.out.println(httpResponse.getFirstHeader("Content-Type"));
		Header[] h= httpResponse.getHeaders("Content-Type");
		for (int i = 0; i < h.length; i++) {
			System.out.println(h[i]);
		}
		//獲取所有頭資訊
		HeaderIterator hi=httpResponse.headerIterator();
		while(hi.hasNext()){
			Header he=hi.nextHeader();
			System.out.println(he.getName()+":"+he.getValue());
		}