zoukankan      html  css  js  c++  java
  • JAVA通用GET和POST方法

    1.先设置请求和超时时间

        /**
         * 读超时设置30分钟
         */
        private static int READTIMEOUT = 1800000;
    
        /**
         * 链接超时设置30秒
         */
        private static int CONNECTTIMEOUT = 30000;

    2.GET请求,ServiceResult封装过:

      /**
         * 通用get方法
         * @param addr
         * @param params
         * @return
         */
        public static ServiceResult<Boolean> sendGet(String addr, Map<String, String> params) {
            String res = "error";
            ServiceResult sr = new ServiceResult(false, res);
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(addr);
            List<NameValuePair> values = Lists.newArrayList();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                values.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            String str = "";
            UrlEncodedFormEntity uefEntity;
            try {
                str = EntityUtils.toString(new UrlEncodedFormEntity(values, Consts.UTF_8));
                LOG.warn("executing request " + addr + "?" + str);
                //设置请求和传输超时时间
                RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(READTIMEOUT).setConnectTimeout(
                        CONNECTTIMEOUT).build();
                httpGet.setConfig(requestConfig);
                CloseableHttpResponse response = httpclient.execute(httpGet);
                try {
                    if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
                        sr.setData(true);
                        sr.setSucceed(true);
                    } else {
                        sr.setSucceed(false);
                        sr.setData(false);
                    }
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        res = EntityUtils.toString(entity, "UTF-8");
                        LOG.warn(res);
                    }
                } finally {
                    response.close();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
                LOG.warn("", e);
            } catch (UnsupportedEncodingException e) {
                LOG.warn("", e);
            } catch (IOException e) {
                LOG.warn("", e);
            } finally {
                // 关闭连接,释放资源
                try {
                    httpclient.close();
                } catch (IOException e) {
                    LOG.warn("", e);
                }
            }
            return sr;
        }

    3.POST请求:

      /**
         * 通用post方法
         * @param addr
         * @param params
         * @return
         */
        public static ServiceResult<Boolean> sendPost(String addr, Map<String, String> params) {
            String res = "error";
            ServiceResult sr = new ServiceResult(false, res);
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httppost = new HttpPost(addr);
            List formparams = new ArrayList();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            UrlEncodedFormEntity uefEntity;
            try {
                uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
                httppost.setEntity(uefEntity);
                LOG.warn("executing request " + httppost.getURI());
                //设置请求和传输超时时间
                RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(READTIMEOUT).setConnectTimeout(
                        CONNECTTIMEOUT).build();
                httppost.setConfig(requestConfig);
                CloseableHttpResponse response = httpclient.execute(httppost);
                try {
                    if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
                        sr.setData(true);
                        sr.setSucceed(true);
                    } else {
                        sr.setSucceed(false);
                        sr.setData(false);
                    }
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        res = EntityUtils.toString(entity, "UTF-8");
                        sr.setMsg(res);
                    }
                } finally {
                    response.close();
                }
            } catch (ClientProtocolException e) {
                LOG.error("", e);
            } catch (UnsupportedEncodingException e) {
                LOG.error("", e);
            } catch (IOException e) {
                LOG.error("", e);
            } finally {
                // 关闭连接,释放资源
                try {
                    httpclient.close();
                } catch (IOException e) {
                    LOG.error("", e);
                }
            }
            return sr;
        }
    随笔看心情
  • 相关阅读:
    JavaScript中判断函数是new还是()调用
    IE6/7 and IE8/9(Q)中td的上下padding失效
    JQuery中html()方法使用不当带来的陷阱
    有name为action的表单元素时取form的属性action杯具了
    为非IE浏览器添加mouseenter,mouseleave事件
    各浏览器中querySelector和querySelectorAll的实现差异
    仅IE6/7/8中innerHTML返回值忽略英文空格
    各浏览器关键字/保留字作为对象属性的差异
    各浏览器中鼠标按键值的差异
    给body标签和document.body都添加点击事件后仅Firefox弹出了两次
  • 原文地址:https://www.cnblogs.com/stromgao/p/12156029.html
Copyright © 2011-2022 走看看