|
@@ -0,0 +1,116 @@
|
|
|
+package com.ygj.yuemum.client;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import springfox.documentation.spring.web.json.Json;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class HttpClientUtil {
|
|
|
+
|
|
|
+ protected final static String DEFAULT_USERNAME = "admin";
|
|
|
+
|
|
|
+ protected final static String DEFAULT_PASSWORD = "e19d5cd5af0378da05f63f891c7467af";
|
|
|
+
|
|
|
+ protected final static String DEFAULT_URL = "http://localhost:8888/";
|
|
|
+
|
|
|
+ public static String post(String url , Map<String, String> params){
|
|
|
+ try {
|
|
|
+ String jsonObject = postByToken(url, params, getToken(), true);
|
|
|
+ return jsonObject;
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new String();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String get(String url, Map<String, String> params) {
|
|
|
+ try {
|
|
|
+ String jsonObject = getByToken(url, params);
|
|
|
+ return jsonObject;
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new String();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getByToken(String url, Map<String, String> params) throws IOException {
|
|
|
+ CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
+ HttpGet httpGet = new HttpGet(DEFAULT_URL + url);
|
|
|
+ httpGet.setHeader("JSESSIONID", getToken());
|
|
|
+ CloseableHttpResponse response1 = httpclient.execute(httpGet);
|
|
|
+
|
|
|
+ try {
|
|
|
+ HttpEntity entity1 = response1.getEntity();
|
|
|
+ // do something useful with the response body
|
|
|
+ // and ensure it is fully consumed
|
|
|
+ String jsonObject = EntityUtils.toString(entity1, "UTF-8");
|
|
|
+ EntityUtils.consume(entity1);
|
|
|
+ return jsonObject;
|
|
|
+ } finally {
|
|
|
+ response1.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String postNoToken(String url,Map<String,String> params) throws IOException {
|
|
|
+ return postByToken(url, params, null, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String postByToken(String url, Map<String,String> params, String token, boolean isJson) throws IOException {
|
|
|
+ CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(DEFAULT_URL + url);
|
|
|
+ List<NameValuePair> nvps = new ArrayList<>();
|
|
|
+ if (params != null) {
|
|
|
+ params.forEach((k, v) -> nvps.add(new BasicNameValuePair(k, v)));
|
|
|
+ }
|
|
|
+ if (isJson) {
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+ httpPost.setEntity(new StringEntity(JSON.toJSONString(params)));
|
|
|
+ } else {
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps));
|
|
|
+ }
|
|
|
+ httpPost.setHeader("Accept", "application/json, text/plain, */*");
|
|
|
+ if (StringUtils.isNotBlank(token)) {
|
|
|
+ httpPost.setHeader("JSESSIONID", token);
|
|
|
+ }
|
|
|
+ CloseableHttpResponse response2 = httpclient.execute(httpPost);
|
|
|
+
|
|
|
+ try {
|
|
|
+ System.out.println(response2.getStatusLine());
|
|
|
+ HttpEntity entity2 = response2.getEntity();
|
|
|
+ String jsonObject = EntityUtils.toString(entity2, "UTF-8");
|
|
|
+ EntityUtils.consume(entity2);
|
|
|
+ return jsonObject;
|
|
|
+ } finally {
|
|
|
+ response2.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String getToken() throws IOException {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("username", DEFAULT_USERNAME);
|
|
|
+ params.put("password", DEFAULT_PASSWORD);
|
|
|
+ params.put("pwd", "abcd1234");
|
|
|
+ params.put("rememberMe", "false");
|
|
|
+ JSONObject jsonObject = JSON.parseObject(postNoToken("login", params));
|
|
|
+ return (String) jsonObject.get("token");
|
|
|
+ }
|
|
|
+}
|