http get请求

妖狐艹你老母 2022-08-20 14:09 372阅读 0赞

http get 请求方式

代码

  1. import java.io.IOException;
  2. import java.net.URI;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpStatus;
  10. import org.apache.http.client.config.RequestConfig;
  11. import org.apache.http.client.methods.CloseableHttpResponse;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.util.EntityUtils;
  16. /**
  17. *
  18. * GET接口服务的JAVA示例
  19. *
  20. */
  21. public class HttpGetApiInvoker {
  22. private final static String URL = "http://test/url";
  23. private final static String REQUEST_CHARSET = "UTF-8";
  24. private final static String RESPONSE_CHARSET = "UTF-8";
  25. private final static int CONNECT_TIMEOUT = 5000; //调用连接超时时间
  26. private final static int REQUEST_TIMEOUT = 10000; //调用获取数据超时时间
  27. public static void main(String[] args) throws Exception{
  28. HttpGetApiInvoker httpGetApiInvoker= new HttpGetApiInvoker();
  29. Map<String, Object> parameterList = httpGetApiInvoker.prepareParameter();
  30. String result = httpGetApiInvoker.invoke(parameterList);
  31. System.out.println(result);
  32. }
  33. /***
  34. * 准备业务参数
  35. * @return
  36. */
  37. private Map<String, Object> prepareParameter(){
  38. Map<String, Object> parameterMap = new HashMap<>();
  39. parameterMap.put("name", "xxx");
  40. parameterMap.put("password", "xxxxxxxx");
  41. return parameterMap;
  42. }
  43. /***
  44. * 拼接调用的URL
  45. * @param url
  46. * @param parametersMap 业务数据,作为参数
  47. * @return
  48. */
  49. private URI buildUri(String url, Map<String, Object> parametersMap) {
  50. if (null == parametersMap || parametersMap.isEmpty()) {
  51. return URI.create(url);
  52. }
  53. ArrayList list = new ArrayList(parametersMap.size());
  54. Iterator iterator = parametersMap.entrySet().iterator();
  55. while (iterator.hasNext()) {
  56. Map.Entry entry = (Map.Entry) iterator.next();
  57. list.add(entry.getKey().toString().trim() + "=" + entry.getValue().toString().trim());
  58. }
  59. return list.isEmpty() ? URI.create(url) : URI.create(url + "?" + StringUtils.join(list, "&"));
  60. }
  61. /***
  62. * 没有使用长连接
  63. * @param parameterMap
  64. * @return JSON格式的字符串
  65. * @throws IOException
  66. */
  67. private String invoke(Map<String, Object> parameterMap) throws IOException {
  68. HttpGet httpGet = null;
  69. String result = "";
  70. try {
  71. //拼接URL
  72. httpGet = new HttpGet(buildUri(URL,parameterMap));
  73. //设置超时时间
  74. RequestConfig requestConfig = RequestConfig.custom()
  75. .setConnectTimeout(CONNECT_TIMEOUT)
  76. .setSocketTimeout(REQUEST_TIMEOUT)
  77. .build();
  78. httpGet.setConfig(requestConfig);
  79. httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + REQUEST_CHARSET);
  80. CloseableHttpClient httpClient = HttpClients.createDefault();
  81. CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
  82. HttpEntity httpEntity = httpResponse.getEntity();
  83. //返回值不是200,进行错误处理
  84. if(HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()){
  85. //相关处理
  86. if(null != httpGet) {
  87. httpGet.abort();
  88. }
  89. return result;
  90. }
  91. //获取结果
  92. result = EntityUtils.toString(httpEntity, RESPONSE_CHARSET);
  93. }catch(Exception e){
  94. if(null != httpGet){
  95. httpGet.abort();
  96. }
  97. throw e;
  98. }
  99. return result;
  100. }
  101. }

项目依赖

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. <version>3.3.2 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.httpcomponents</groupId>
  8. <artifactId>httpclient</artifactId>
  9. <version> 4.3.6</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.httpcomponents</groupId>
  13. <artifactId>httpcore</artifactId>
  14. <version>4.4</version>
  15. </dependency>

发表评论

表情:
评论列表 (有 0 条评论,372人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Http get方式发送请求

    首先介绍一种简单的方式,因为get请求传递参数是可以直接拼凑在Url后的,这个是最基本的东西,即使有各种的工具类,本质还是这个,我们先把最本质的东西贴出来,接下来再介绍一些工具

    相关 http GET 请求 URL 总结

    URL 只能使用英文字母、阿拉伯数字和某些标点符号,不能使用其他文字和符号。网络标准[RFC 1738][]做了硬性规定: > “…Only alphanumerics \[