HkHttpUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.sw.patroleditor.util;
  2. import com.sw.patroleditor.domain.model.BodyRequestModel;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.auth.AuthScope;
  5. import org.apache.http.auth.UsernamePasswordCredentials;
  6. import org.apache.http.client.CredentialsProvider;
  7. import org.apache.http.client.methods.*;
  8. import org.apache.http.entity.StringEntity;
  9. import org.apache.http.impl.client.BasicCredentialsProvider;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import org.apache.http.util.EntityUtils;
  13. public class HkHttpUtils {
  14. /**
  15. * get请求
  16. *
  17. * @param config
  18. * @return
  19. * @throws Exception
  20. */
  21. public String doGet(BodyRequestModel config) throws Exception {
  22. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  23. AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
  24. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
  25. credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
  26. CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
  27. HttpGet httpGet = new HttpGet(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
  28. CloseableHttpResponse responseBody = httpclient.execute(httpGet);
  29. HttpEntity responseEntity = responseBody.getEntity();
  30. if (responseEntity != null) {
  31. String response = EntityUtils.toString(responseEntity);
  32. return response;
  33. }
  34. return null;
  35. }
  36. /**
  37. * delete请求
  38. *
  39. * @param config
  40. * @return
  41. * @throws Exception
  42. */
  43. public String doDelete(BodyRequestModel config) throws Exception {
  44. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  45. AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
  46. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
  47. credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
  48. CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
  49. HttpDelete httpDelete = new HttpDelete(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
  50. CloseableHttpResponse responseBody = httpclient.execute(httpDelete);
  51. HttpEntity responseEntity = responseBody.getEntity();
  52. if (responseEntity != null) {
  53. String response = EntityUtils.toString(responseEntity);
  54. return response;
  55. }
  56. return null;
  57. }
  58. /**
  59. * body方式发起post请求
  60. *
  61. * @param config
  62. * @return
  63. * @throws Exception
  64. */
  65. public String doBodyPost(BodyRequestModel config) throws Exception {
  66. String entity = config.getEntity();
  67. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  68. AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
  69. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
  70. credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
  71. CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
  72. HttpPost httpPost = new HttpPost(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
  73. httpPost.setEntity(new StringEntity(entity, "UTF-8"));
  74. CloseableHttpResponse responseBody = httpclient.execute(httpPost);
  75. HttpEntity responseEntity = responseBody.getEntity();
  76. if (responseEntity != null) {
  77. String response = EntityUtils.toString(responseEntity);
  78. return response;
  79. }
  80. return null;
  81. }
  82. /**
  83. * body方式发起post请求
  84. *
  85. * @param config
  86. * @return
  87. * @throws Exception
  88. */
  89. public String doBodyPut(BodyRequestModel config) throws Exception {
  90. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  91. AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort());
  92. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
  93. credsProvider.setCredentials(favourite_digest_realm, usernamePasswordCredentials);
  94. CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
  95. HttpPut HttpPut = new HttpPut(config.getAgreement() + "://" + config.getIp() + ":" + config.getPort() + config.getUri());
  96. HttpPut.setEntity(new StringEntity(config.getEntity(), "UTF-8"));
  97. CloseableHttpResponse responseBody = httpclient.execute(HttpPut);
  98. HttpEntity responseEntity = responseBody.getEntity();
  99. if (responseEntity != null) {
  100. String response = EntityUtils.toString(responseEntity);
  101. return response;
  102. }
  103. return null;
  104. }
  105. }