HttpClient学习之二

来源:互联网 发布:取消激活windows 编辑:程序博客网 时间:2024/05/16 01:11

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

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 {privateHttpClientBuilder 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 ){// 创建HttpClientBuilderhttpClientBuilder = HttpClientBuilder.create();// HttpClientcloseableHttpClient = httpClientBuilder.build();}return closeableHttpClient;}public static void main(String args[])  {String host = "10.104.203.134";int port = 8080;String URI = "/redfish/v1/PCIeSwitches";// 创建HttpClientBuilderHttpClientBuilder httpClientBuilder = HttpClientBuilder.create();// HttpClientCloseableHttpClient 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 {privateHttpClientBuilder 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 ){// 创建HttpClientBuilderhttpClientBuilder = HttpClientBuilder.create();// HttpClientcloseableHttpClient = 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
原创粉丝点击