将上一个代码加以简单的修改,使之符合自己的需求:
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();
}
}