|
@@ -0,0 +1,117 @@
|
|
|
|
+package com.sw.patroleditor.util;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.sw.patroleditor.domain.model.BodyRequestModel;
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.auth.AuthScope;
|
|
|
|
+import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
|
+import org.apache.http.client.CredentialsProvider;
|
|
|
|
+import org.apache.http.client.methods.*;
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
+import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+public class HkHttpUtils {
|
|
|
|
+ /**
|
|
|
|
+ * get请求
|
|
|
|
+ *
|
|
|
|
+ * @param config
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public String doGet(BodyRequestModel config) throws Exception {
|
|
|
|
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
|
+ AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
|
|
|
|
+ UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
|
|
|
|
+ credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
|
|
|
|
+ CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
|
|
|
+ HttpGet httpGet = new HttpGet(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
|
|
|
|
+
|
|
|
|
+ CloseableHttpResponse responseBody = httpclient.execute(httpGet);
|
|
|
|
+ HttpEntity responseEntity = responseBody.getEntity();
|
|
|
|
+ if (responseEntity != null) {
|
|
|
|
+ String response = EntityUtils.toString(responseEntity);
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * delete请求
|
|
|
|
+ *
|
|
|
|
+ * @param config
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public String doDelete(BodyRequestModel config) throws Exception {
|
|
|
|
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
|
+ AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
|
|
|
|
+ UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
|
|
|
|
+ credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
|
|
|
|
+ CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
|
|
|
+ HttpDelete httpDelete = new HttpDelete(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
|
|
|
|
+ CloseableHttpResponse responseBody = httpclient.execute(httpDelete);
|
|
|
|
+ HttpEntity responseEntity = responseBody.getEntity();
|
|
|
|
+ if (responseEntity != null) {
|
|
|
|
+ String response = EntityUtils.toString(responseEntity);
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * body方式发起post请求
|
|
|
|
+ *
|
|
|
|
+ * @param config
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public String doBodyPost(BodyRequestModel config) throws Exception {
|
|
|
|
+ String entity = config.getEntity();
|
|
|
|
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
|
+ AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
|
|
|
|
+ UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
|
|
|
|
+ credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
|
|
|
|
+ CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
|
|
|
+ HttpPost httpPost = new HttpPost(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
|
|
|
|
+ httpPost.setEntity(new StringEntity(entity, "UTF-8"));
|
|
|
|
+ CloseableHttpResponse responseBody = httpclient.execute(httpPost);
|
|
|
|
+ HttpEntity responseEntity = responseBody.getEntity();
|
|
|
|
+ if (responseEntity != null) {
|
|
|
|
+ String response = EntityUtils.toString(responseEntity);
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * body方式发起post请求
|
|
|
|
+ *
|
|
|
|
+ * @param config
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public String doBodyPut(BodyRequestModel config) throws Exception {
|
|
|
|
+ String entity = config.getEntity();
|
|
|
|
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
|
+ AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
|
|
|
|
+ UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
|
|
|
|
+ credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
|
|
|
|
+ CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
|
|
|
|
+ HttpPut HttpPut = new HttpPut(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
|
|
|
|
+ HttpPut.setEntity(new StringEntity(entity, "UTF-8"));
|
|
|
|
+ CloseableHttpResponse responseBody = httpclient.execute(HttpPut);
|
|
|
|
+ HttpEntity responseEntity = responseBody.getEntity();
|
|
|
|
+ if (responseEntity != null) {
|
|
|
|
+ String response = EntityUtils.toString(responseEntity);
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|