packagecom.inspur.demo.http.client;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.nio.charset.Charset;importjava.util.ArrayList;importjava.util.List;importorg.apache.http.HttpEntity;importorg.apache.http.NameValuePair;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.fluent.Form;importorg.apache.http.client.fluent.Request;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.ContentType;importorg.apache.http.entity.InputStreamEntity;importorg.apache.http.entity.StringEntity;importorg.apache.http.entity.mime.MultipartEntityBuilder;importorg.apache.http.entity.mime.content.FileBody;importorg.apache.http.entity.mime.content.StringBody;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;/*** 通过HttpClient调用Http接口*/
public classHttpClientCase {/*** GET请求*/
public static voidget() {
String requestPath= "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白";
CloseableHttpClient httpClient=HttpClients.createDefault();try{
HttpGet get= newHttpGet(requestPath);
CloseableHttpResponse response=httpClient.execute(get);
System.out.println("GET返回状态:" +response.getStatusLine());
HttpEntity responseEntity=response.getEntity();
System.out.println("GET返回结果:" +EntityUtils.toString(responseEntity));//流畅api调用
String result = Request.Get(requestPath).execute().returnContent().asString(Charset.forName("utf-8"));
System.out.println("GET fluent返回结果:" +result);
}catch(Exception e) {
e.printStackTrace();
}finally{
close(httpClient);
}
}/*** POST请求(发送键值对数据)*/
public static voidpost() {
String requestPath= "http://localhost:8080/demo/httptest/getUser";
CloseableHttpClient httpClient=HttpClients.createDefault();try{
HttpPost post= newHttpPost(requestPath);
List list = new ArrayList();
list.add(new BasicNameValuePair("userId", "1000"));
list.add(new BasicNameValuePair("userName", "李白"));
post.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
CloseableHttpResponse response=httpClient.execute(post);
System.out.println("POST返回状态:" +response.getStatusLine());
HttpEntity responseEntity=response.getEntity();
System.out.println("POST返回结果:" +EntityUtils.toString(responseEntity));//流畅api调用
String result =Request.Post(requestPath)
.bodyForm(Form.form().add("userId", "1000").add("userName", "李白").build(), Charset.forName("utf-8"))
.execute().returnContent().asString(Charset.forName("utf-8"));
System.out.println("POST fluent返回结果:" +result);
}catch(Exception e) {
e.printStackTrace();
}finally{
close(httpClient);
}
}/*** POST请求(发送json数据)*/
public static voidpost2() {
String requestPath= "http://localhost:8080/demo/httptest/addUser";
CloseableHttpClient httpClient=HttpClients.createDefault();try{
HttpPost post= newHttpPost(requestPath);
post.setHeader("Content-type", "application/json");
String param= "{\"userId\": \"1001\",\"userName\":\"杜甫\"}";
post.setEntity(new StringEntity(param, "utf-8"));
CloseableHttpResponse response=httpClient.execute(post);
System.out.println("POST json返回状态:" +response.getStatusLine());
HttpEntity responseEntity=response.getEntity();
System.out.println("POST josn返回结果:" +EntityUtils.toString(responseEntity));//流畅api调用
String result =Request.Post(requestPath)
.addHeader("Content-type", "application/json")
.bodyString(param, ContentType.APPLICATION_JSON)
.execute().returnContent().asString(Charset.forName("utf-8"));
System.out.println("POST json fluent返回结果:" +result);
}catch(Exception e) {
e.printStackTrace();
}finally{
close(httpClient);
}
}/*** 上传文件*/
public static voidupload() {
String requestPath= "http://localhost:8080/demo/httptest/upload";
CloseableHttpClient httpClient=HttpClients.createDefault();try{
HttpPost post= newHttpPost(requestPath);
FileInputStream fileInputStream= new FileInputStream("d:/a.jpg");
post.setEntity(newInputStreamEntity(fileInputStream));
CloseableHttpResponse response=httpClient.execute(post);
System.out.println("upload返回状态:" +response.getStatusLine());
HttpEntity responseEntity=response.getEntity();
System.out.println("upload返回结果:" +EntityUtils.toString(responseEntity));//流畅api调用
String result =Request.Post(requestPath)
.bodyStream(new FileInputStream("d:/a.jpg"))
.execute().returnContent().asString(Charset.forName("utf-8"));
System.out.println("upload fluent返回结果:" +result);
}catch(Exception e) {
e.printStackTrace();
}finally{
close(httpClient);
}
}/*** 上传文件及发送键值对数据*/
public static voidmulti() {
String requestPath= "http://localhost:8080/demo/httptest/multi";
CloseableHttpClient httpClient=HttpClients.createDefault();try{
HttpPost post= newHttpPost(requestPath);
FileBody file= new FileBody(new File("d:/a.jpg"));
HttpEntity requestEntity=MultipartEntityBuilder.create()
.addPart("file", file)
.addPart("param1", new StringBody("参数1", ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "utf-8")))
.addPart("param2", new StringBody("参数2", ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "utf-8")))
.build();
post.setEntity(requestEntity);
CloseableHttpResponse response=httpClient.execute(post);
System.out.println("multi返回状态:" +response.getStatusLine());
HttpEntity responseEntity=response.getEntity();
System.out.println("multi返回结果:" +EntityUtils.toString(responseEntity));//流畅api调用
String result =Request.Post(requestPath)
.body(MultipartEntityBuilder.create()
.addPart("file", file)
.addPart("param1", new StringBody("参数1", ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "utf-8")))
.addPart("param2", new StringBody("参数2", ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "utf-8")))
.build())
.execute().returnContent().asString(Charset.forName("utf-8"));
System.out.println("multi fluent返回结果:" +result);
}catch(Exception e) {
e.printStackTrace();
}finally{
close(httpClient);
}
}private static voidclose(CloseableHttpClient httpClient) {try{if (httpClient != null) {
httpClient.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}public static voidmain(String[] args) {
get();
post();
post2();
upload();
multi();
}
}