HttpClient学习之二

将上一个代码加以简单的修改,使之符合自己的需求:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class SimpleClient {
	
	private	HttpClientBuilder httpClientBuilder;
	private CloseableHttpClient closeableHttpClient;

	/**
	 * 处理单个get请求,返回结果
	 * @param closeableHttpClient
	 * @param host
	 * @param port
	 * @param URI
	 * @return
	 */
	public String doGetMethod(CloseableHttpClient closeableHttpClient, String host, int port, String URI){
		String result = "";
		HttpGet httpGet = null;
		HttpResponse httpResponse = null;
		HttpEntity entity = null;
		httpGet = new HttpGet("http://"+host+":"+port+URI);
		try {
			httpResponse = closeableHttpClient.execute(httpGet);
			entity = httpResponse.getEntity();
			if( entity != null ){
				result = EntityUtils.toString(entity);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
		return result;
	}
	
	/**
	 * 处理多个get请求,返回List
	 * @param closeableHttpClient
	 * @param host
	 * @param port
	 * @param URIList
	 * @return
	 */
	public List<String> doGetMethodBatch(CloseableHttpClient closeableHttpClient, String host, int port, List<String> URIList){
		List<String> resultList = new ArrayList<String> ();
		HttpGet httpGet = null;
		HttpResponse httpResponse = null;
		HttpEntity entity = null;
		String content = "";
		for( String URI : URIList ){
			httpGet = new HttpGet("http://"+host+":"+port+URI);
			try {
				httpResponse = closeableHttpClient.execute(httpGet);
				entity = httpResponse.getEntity();
				if( entity != null ){
					content = EntityUtils.toString(entity);
					resultList.add(content);
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return resultList;
	}
	
	public void closeHttpClient(CloseableHttpClient closeableHttpClient){
		if( closeableHttpClient != null ){
			try {
				closeableHttpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public CloseableHttpClient createCloseableHttpClient(){
		if( closeableHttpClient != null ){
			// 创建HttpClientBuilder
			httpClientBuilder = HttpClientBuilder.create();
			// HttpClient
			closeableHttpClient = httpClientBuilder.build();
		}
		return closeableHttpClient;
	}
	
	public static void main(String args[])  {
		String host = "10.104.203.134";
		int port = 8080;
		String URI = "/redfish/v1/PCIeSwitches";
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		
		SimpleClient simpleClient = new SimpleClient();
		System.out.println(simpleClient.doGetMethod(closeableHttpClient, host, port, URI));
		List<String> URIList = new ArrayList<String>();
		URIList.add("/redfish/v1/PCIeSwitches");
		URIList.add("/redfish/v1/PCIeSwitches/1");
		URIList.add("/redfish/v1/PCIeSwitches/1/PCIePorts");
		URIList.add("/redfish/v1/PCIeSwitches/1/PCIeZones");
		System.out.println(simpleClient.doGetMethodBatch(closeableHttpClient, host, port, URIList));
		simpleClient.closeHttpClient(closeableHttpClient);
		
	}
再修改一下,形成一个简单的工具类:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class SimpleHttpClient {
	
	private	HttpClientBuilder httpClientBuilder;
	private CloseableHttpClient closeableHttpClient;

	public String doGetMethod(String host, int port, String URI){
		if( closeableHttpClient == null ){
			createCloseableHttpClient();
		}
		String result = "";
		HttpGet httpGet = null;
		HttpResponse httpResponse = null;
		HttpEntity entity = null;
		httpGet = new HttpGet("http://"+host+":"+port+URI);
		try {
			httpResponse = closeableHttpClient.execute(httpGet);
			entity = httpResponse.getEntity();
			if( entity != null ){
				result = EntityUtils.toString(entity);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
		return result;
	}
	
	public List<String> doGetMethodBatch(String host, int port, List<String> URIList){
		if( closeableHttpClient == null ){
			createCloseableHttpClient();
		}
		List<String> resultList = new ArrayList<String> ();
		HttpGet httpGet = null;
		HttpResponse httpResponse = null;
		HttpEntity entity = null;
		String content = "";
		for( String URI : URIList ){
			httpGet = new HttpGet("http://"+host+":"+port+URI);
			try {
				httpResponse = closeableHttpClient.execute(httpGet);
				entity = httpResponse.getEntity();
				if( entity != null ){
					content = EntityUtils.toString(entity);
					resultList.add(content);
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return resultList;
	}
	
	public void closeHttpClient(){
		if( closeableHttpClient != null ){
			try {
				closeableHttpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void createCloseableHttpClient(){
		if( closeableHttpClient == null ){
			// 创建HttpClientBuilder
			httpClientBuilder = HttpClientBuilder.create();
			// HttpClient
			closeableHttpClient = httpClientBuilder.build();
		}
	}
	
	public static void main(String args[])  {
		String host = "10.104.203.134";
		int port = 8080;
		String URI = "/redfish/v1/PCIeSwitches";
		
		SimpleHttpClient simpleHttpClient = new SimpleHttpClient();
		System.out.println(simpleHttpClient.doGetMethod(host, port, URI));
		List<String> URIList = new ArrayList<String>();
		URIList.add("/redfish/v1/PCIeSwitches");
		URIList.add("/redfish/v1/PCIeSwitches/1");
		URIList.add("/redfish/v1/PCIeSwitches/1/PCIePorts");
		URIList.add("/redfish/v1/PCIeSwitches/1/PCIeZones");
		System.out.println(simpleHttpClient.doGetMethodBatch( host, port, URIList));
		simpleHttpClient.closeHttpClient();
		
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值