123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 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 {
- 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(config.getEntity(), "UTF-8"));
- CloseableHttpResponse responseBody = httpclient.execute(HttpPut);
- HttpEntity responseEntity = responseBody.getEntity();
- if (responseEntity != null) {
- String response = EntityUtils.toString(responseEntity);
- return response;
- }
- return null;
- }
- }
|