在pom.xml增加httpclient

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>com.example</groupId>
    5. <artifactId>json</artifactId>
    6. <packaging>jar</packaging>
    7. <version>1.0-SNAPSHOT</version>
    8. <name>json</name>
    9. <url>http://maven.apache.org</url>
    10. <dependencies>
    11. <dependency>
    12. <groupId>junit</groupId>
    13. <artifactId>junit</artifactId>
    14. <version>3.8.1</version>
    15. <scope>test</scope>
    16. </dependency>
    17. <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    18. <dependency>
    19. <groupId>org.apache.httpcomponents</groupId>
    20. <artifactId>httpclient</artifactId>
    21. <version>4.5.6</version>
    22. </dependency>
    23. </dependencies>
    24. </project>

    封装操作类

    1. package com.example;
    2. import org.apache.http.HttpResponse;
    3. import org.apache.http.NameValuePair;
    4. import org.apache.http.client.HttpClient;
    5. import org.apache.http.client.config.RequestConfig;
    6. import org.apache.http.client.entity.UrlEncodedFormEntity;
    7. import org.apache.http.client.methods.HttpGet;
    8. import org.apache.http.client.methods.HttpPost;
    9. import org.apache.http.impl.client.HttpClientBuilder;
    10. import org.apache.http.message.BasicNameValuePair;
    11. import org.apache.http.util.EntityUtils;
    12. import java.util.ArrayList;
    13. import java.util.List;
    14. import java.util.Map;
    15. /**
    16. * HttpClient工具类
    17. */
    18. public class HttpUtil {
    19. //private static Logger logger = Logger.getLogger(HttpUtil.class);
    20. /**
    21. * get请求
    22. * @return
    23. */
    24. public static String get(String url,Map<String,String>headers){
    25. String result = "";
    26. try {
    27. // 构造httprequest设置
    28. RequestConfig config = RequestConfig.custom().setConnectTimeout(5)
    29. .setConnectionRequestTimeout(10).build();
    30. HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
    31. HttpGet htGet = new HttpGet(url);
    32. // 添加http headers
    33. if (headers != null && headers.size() > 0) {
    34. for (String key : headers.keySet()) {
    35. htGet.addHeader(key, headers.get(key));
    36. }
    37. }
    38. // 读取数据
    39. HttpResponse response = client.execute(htGet);
    40. result = EntityUtils.toString(response.getEntity(), "utf-8");
    41. } catch (Exception e) {
    42. System.out.println("发送GET请求出现异常!" + e);
    43. e.printStackTrace();
    44. }
    45. return result;
    46. }
    47. /*
    48. 不带参数请求
    49. */
    50. public static String get(String url) {
    51. return get(url,null);
    52. }
    53. /**
    54. * post请求(用于key-value格式的参数)
    55. * @param url
    56. * @param param
    57. * @return
    58. */
    59. public static String post(String url, Map<String, String> param) {
    60. return post(url,param,null);
    61. }
    62. public static String post(String url,Map<String,String> param,Map<String,String> headers){
    63. String result="";
    64. try {
    65. // 构造httprequest设置
    66. RequestConfig config = RequestConfig.custom().setConnectTimeout(5)
    67. .setConnectionRequestTimeout(10).build();
    68. HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
    69. HttpPost httpPost = new HttpPost(url);
    70. if (param != null) {
    71. List<NameValuePair> paramList = new ArrayList<NameValuePair>();
    72. for (String key : param.keySet()) {
    73. paramList.add(new BasicNameValuePair(key, param.get(key)));
    74. }
    75. // 模拟表单
    76. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
    77. httpPost.setEntity(entity);
    78. }
    79. // 添加http headers
    80. if (headers != null && headers.size() > 0) {
    81. for (String key : headers.keySet()) {
    82. httpPost.addHeader(key, headers.get(key));
    83. }
    84. }
    85. // 读取数据
    86. HttpResponse response = client.execute(httpPost);
    87. result = EntityUtils.toString(response.getEntity(), "utf-8");
    88. } catch (Exception e) {
    89. System.out.println("发送GET请求出现异常!" + e);
    90. e.printStackTrace();
    91. }
    92. return result;
    93. }
    94. }