zoukankan      html  css  js  c++  java
  • HttpClient学习

      /**
         * 使用GET方式请求 最简单的物流追踪方式,如果某些物流公司的获取追踪信息的方式与此相同,则可以直接调用次方法
         *
         * @param requestUrl
         * @return 追踪信息
         */
        public static String getTrackInfo(String requestUrl) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(requestUrl);
            try {
                CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    System.out.println("Method is wrong " + httpResponse.getStatusLine());
                    return null;
                }
                return EntityUtils.toString(httpResponse.getEntity());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    httpGet.releaseConnection();
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        /**
         * 使用POST方式请求 最简单的物流追踪方式,如果某些物流公司的获取追踪信息的方式与此相同,则可以直接调用次方法
         *
         * @param requestUrl
         * @return 追踪信息
         */
        public String postTrackInfo(String requestUrl, Map<String, String> params) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(requestUrl);
            try {
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                if (params != null) {
                    for (Entry<String, String> entry : params.entrySet()) {
                        nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                    }
                }
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
                httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
                HttpResponse httpResponse = httpClient.execute(httpPost);
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    System.out.println("Method is wrong " + httpResponse.getStatusLine());
                    return null;
                }
                return EntityUtils.toString(httpResponse.getEntity());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    httpPost.releaseConnection();
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

  • 相关阅读:
    王颖奇 201771010129《面向对象程序设计(java)》第七周学习总结
    王颖奇 201771010129《面向对象程序设计(java)》第六周学习总结
    王颖奇 201771010129《面向对象程序设计(java)》第四周学习总结
    王颖奇 201771010129 第三周 Java基本程序设计总结
    《三带一队》【Alpha】Scrum meeting 3
    《三带一队》【Alpha】Scrum meeting 2
    《三带一队》【Alpha】Scrum meeting 1
    三带一队 实验八 团队作业4:团队项目需求建模与系统设计
    三带一队 实验七 团队作业3:团队项目需求分析与原型设计
    三带一队 实验六团队作业2:麋鹿寻途
  • 原文地址:https://www.cnblogs.com/zhc-hnust/p/6532530.html
Copyright © 2011-2022 走看看