1. package com.jt.util;
    2. import java.io.IOException;
    3. import java.io.UnsupportedEncodingException;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.Map;
    7. import org.apache.http.HttpEntity;
    8. import org.apache.http.NameValuePair;
    9. import org.apache.http.client.config.RequestConfig;
    10. import org.apache.http.client.entity.UrlEncodedFormEntity;
    11. import org.apache.http.client.methods.CloseableHttpResponse;
    12. import org.apache.http.client.methods.HttpGet;
    13. import org.apache.http.client.methods.HttpPost;
    14. import org.apache.http.impl.client.CloseableHttpClient;
    15. import org.apache.http.message.BasicNameValuePair;
    16. import org.apache.http.util.EntityUtils;
    17. import org.springframework.beans.factory.annotation.Autowired;
    18. import org.springframework.stereotype.Service;
    19. import org.springframework.util.StringUtils;
    20. @Service
    21. public class HttpClientService {
    22. @Autowired
    23. private CloseableHttpClient httpClient;
    24. @Autowired
    25. private RequestConfig requestConfig;
    26. //重载几个工具类
    27. public String doGet(String url) {
    28. return doGet(url,null,null);
    29. }
    30. public String doGet(String url,Map<String, String> params) {
    31. return doGet(url,params,null);
    32. }
    33. public String doGet(String url, Map<String, String> params, String charset){
    34. // 1.检验字符集编码格式
    35. charset = StringUtils.isEmpty(charset)?"UTF-8":charset;
    36. String result=null;
    37. // 2.校验map集合是否为null
    38. if (params != null) {
    39. url+="?";
    40. for (Map.Entry<String, String> entry : params.entrySet()) {
    41. String key = entry.getKey();
    42. String value = entry.getValue();
    43. url = url + key + "=" + value + "&";
    44. }
    45. // 经过循环之后多&符
    46. url = url.substring(0, url.length() - 1);
    47. }
    48. HttpGet httpGet = new HttpGet(url);
    49. httpGet.setConfig(requestConfig); //设定超时时间
    50. try {
    51. CloseableHttpResponse response =httpClient.execute(httpGet);
    52. if(response.getStatusLine().getStatusCode() == 200) {
    53. HttpEntity httpEntity = response.getEntity();
    54. result=EntityUtils.toString(httpEntity,charset);
    55. }
    56. } catch (Exception e) {
    57. e.printStackTrace();
    58. throw new RuntimeException(e);//转化成运行时异常
    59. }
    60. return result;
    61. }
    62. public String doPost(String url,Map<String,String> params,String charset){
    63. String result = null;
    64. //1.定义请求类型
    65. HttpPost post = new HttpPost(url);
    66. post.setConfig(requestConfig); //定义超时时间
    67. //2.判断字符集是否为null
    68. if(StringUtils.isEmpty(charset)){
    69. charset = "UTF-8";
    70. }
    71. //3.判断用户是否传递参数
    72. if(params !=null){
    73. //3.2准备List集合信息
    74. List<NameValuePair> parameters = new ArrayList<>();
    75. //3.3将数据封装到List集合中
    76. for (Map.Entry<String,String> entry : params.entrySet()) {
    77. parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    78. }
    79. //3.1模拟表单提交
    80. try {
    81. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,charset); //采用u8编码
    82. //3.4将实体对象封装到请求对象中
    83. post.setEntity(formEntity);//get是拼接字符串,post是setEntity
    84. } catch (UnsupportedEncodingException e) {
    85. e.printStackTrace();
    86. }
    87. }
    88. //4.发送请求
    89. try {
    90. CloseableHttpResponse response = httpClient.execute(post);
    91. //4.1判断返回值状态
    92. if(response.getStatusLine().getStatusCode() == 200) {
    93. result = EntityUtils.toString(response.getEntity(),charset);
    94. }else{
    95. System.out.println("获取状态码信息:"+response.getStatusLine().getStatusCode());
    96. throw new RuntimeException();
    97. }
    98. } catch (IOException e) {
    99. e.printStackTrace();
    100. }
    101. return result;
    102. }
    103. //重载方法
    104. public String doPost(String url){
    105. return doPost(url, null, null);
    106. }
    107. public String doPost(String url,Map<String,String> params){
    108. return doPost(url, params, null);
    109. }
    110. public String doPost(String url,String charset){
    111. return doPost(url, null, charset);
    112. }

    }