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
- HttpClient学习之二
- HttpClient学习(二)
- HttpClient学习(二)
- HttpClient学习(二)
- android学习笔记之HttpClient
- Java Apache HTTPClient Tutorial 深度学习(二)
- 轻松把玩HttpClient之封装HttpClient工具类(二),插件式配置HttpClient对象
- 轻松把玩HttpClient之封装HttpClient工具类(二),插件式配置HttpClient对象
- 轻松把玩HttpClient之封装HttpClient工具类(二),插件式配置HttpClient对象
- 轻松把玩HttpClient之封装HttpClient工具类(二),插件式配置HttpClient对象
- HttpClient(二)
- 【Android进阶学习】Http编程之HttpClient
- android-网络编程之HttpClient学习整理
- Android 学习之--HttpClient详细使用
- 【Android进阶学习】Http编程之HttpClient
- Java4Android之httpclient学习与应用
- 安卓学习之httpclient的应用
- Android学习之HttpClient练习(一)
- iOS企业版分发关于plist和证书的那些事
- Java反编译工具 Eclipse Class Decompiler
- tomcat绑定域名
- 数据结构之排序算法
- strut2简单总结(2)
- HttpClient学习之二
- ios10 相机等权限
- Linux下Apache服务器的搭建
- win32下双缓冲原理及实现
- 光源的基础培训
- MySQL: InnoDB 还是 MyISAM?
- My SQL 函数
- Android多渠道打包实践
- 用 RPM 打包软件 第 2 部分