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;
        }
    随笔看心情
  • 相关阅读:
    azure虚拟机创建后如何ssh私钥连接
    VisualStudio在本地创建新解决方案后如何推送到devops
    【转载】sar命令详解
    【转载】Redis【入门】就这一篇!
    【转载】算法复杂度解析,何为O()
    centOS data格式文件
    linux centOS命令整理
    机器学习中评估计算:PR,ROC,AUC计算方法
    Python中中文输出显示以及列表初始化坑坑
    SKLearn中模型持久化
  • 原文地址:https://www.cnblogs.com/stromgao/p/12156029.html
Copyright © 2011-2022 走看看