HttpClient之GET請求

  JavaEE或者Android的開發者對HttpClient一定不陌生,實際上這是一個在項目中經常被用到的框架,框架本身用法很簡單,網上有很多這方面的資料,這裏只是一個簡單的使用總結。

  當在實際的項目開發中需要作爲客戶端發送HTTP請求時,HttpClient是個不錯的選擇,下面簡單介紹兩種GET請求。

方法一,直接拼接參數

package com.zws.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class ClientGet {
	static String uri = "http://localhost:8080/resteasy/rest/user/";
	static String CHAR_SET = "UTF-8";

	@Test
	public void get0() {
		String path = "getUserDetail2?userId=23&status=1";//直接拼接參數
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet get = new HttpGet(getUri(path));
		CloseableHttpResponse resp = null;
		try {
			resp = client.execute(get);
			int code = resp.getStatusLine().getStatusCode();//響應碼
			System.out.println("resp code:" + code);
			HttpEntity entity = resp.getEntity();
			String msg = EntityUtils.toString(entity, CHAR_SET);//響應信息
			System.out.println("receive msg:" + msg);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (resp != null) {
				try {
					resp.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public String getUri(String path) {
		return uri + path;
	}
}

  方法二,利用URIBuilder輔助構造URI

package com.zws.httpclient;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class ClientGet {
	static String uri = "http://localhost:8080/resteasy/rest/user/";
	static String CHAR_SET = "UTF-8";
	
	@Test
	public void get1() {
		String path = "/resteasy/rest/user/getUserDetail2";
		
		CloseableHttpClient client = HttpClients.createDefault();
		CloseableHttpResponse resp = null;
		try {
			URIBuilder url = new URIBuilder();
			url.setScheme("http");
			url.setHost("localhost:8080");
			url.setPath(path);
			url.setParameter("userId", "23");
			url.setParameter("status", "1");
			
			URI uri = url.build();	
			System.out.println("url:" + uri.toString());
			HttpGet get = new HttpGet(uri);
			
			resp = client.execute(get);
			int code = resp.getStatusLine().getStatusCode();
			System.out.println("resp code:" + code);
			
			HttpEntity entity = resp.getEntity();
			String msg = EntityUtils.toString(entity, CHAR_SET);
			System.out.println("receive msg:" + msg);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (resp != null) {
				try {
					resp.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章