zoukankan      html  css  js  c++  java
  • apache http get 和 post 请求

    1、首先要把jar依赖进项目

    <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.5.3</version>
    </dependency>

    2、get/post方法

    /**
         * http post 请求
         * 1、创建 client 实例
         * 2、创建 post 实例
         * 3、设置 post 参数
         * 4、发送请求
         */
        public static String post(String url, Map<String, String> params) {
            if (url == null) {
                LOGGER.info("http url can not be empty!");
            }
            String respCtn = "";
            // 1、创建 client 实例
            CloseableHttpClient httpclient = HttpClients.createDefault();
            // 2、创建 post 实例
            HttpPost post = new HttpPost(url);
            
            ArrayList<NameValuePair> reqParams = null;
            if (params != null && !params.isEmpty()) {
                reqParams = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> e : params.entrySet()) {
                    reqParams.add(new BasicNameValuePair(e.getKey(), e.getValue()));
                }
            }
    
            HttpResponse response = null;
            try {
                if (reqParams != null)
                    // 3、设置 post 参数
                    post.setEntity(new UrlEncodedFormEntity(reqParams, "UTF-8"));
                // 4、发送请求
                response = httpclient.execute(post);
                respCtn = EntityUtils.toString(response.getEntity());
            } catch (Exception e) {
                LOGGER.error("Fail to connect to remote host [" + url + "]" + e);
            } finally {
                if (httpclient != null) {
                    try {
                        httpclient.close();
                    } catch (IOException e) {
                    }
                }
            }
            return respCtn;
        }
    
    /**
         * http get 请求
         * 
         * @param String
         *            url
         * 
         */
        public static String doGet(String url) {
            if (url == null || url.isEmpty()) {
                LOGGER.info("http url can not be empty!");
            }
            String respCtn = "";
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpResponse response = null;
                HttpGet get = new HttpGet(url);
                response = httpclient.execute(get);
                respCtn = EntityUtils.toString(response.getEntity());
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            } finally {
                if (httpclient != null) {
                    try {
                        httpclient.close();
                    } catch (IOException e) {
                    }
                }
            }
            return respCtn;
        }
  • 相关阅读:
    80端口被NT kernel & System 占用pid= 4的解决方法
    黑马程序员:装饰类的作用——增强被装饰类的方法的功能(利用组合实现复用)
    黑马程序员——java基础之文件复制
    10进制转换16进制原理及取得16进制最后一位或倒数第二位
    Django-admin源码流程
    Django-内置Admin
    Django-Form 补充
    有时间的时候可以看看
    编辑器KindEditor的使用
    Git的使用
  • 原文地址:https://www.cnblogs.com/dannyyao/p/6972702.html
Copyright © 2011-2022 走看看