zoukankan      html  css  js  c++  java
  • HTTP请求工具类

    /**
    *
    * @Description https post请求,参数为字符串
    * @param param
    * @param url
    * @param socketTimeout 毫秒数量
    * @return String
    * @throws Exception
    * @author X Yang
    * @date 2017年6月28日 下午1:28:30
    */

    CloseableHttpClient httpclient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
    String responseData = null;
    try {
    HttpPost request = new HttpPost(url);
    // 解决中文乱码问题
    StringEntity entity = new StringEntity(requestData, "utf-8");
    entity.setContentType("application/json");
    request.setEntity(entity);
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(360000).setConnectTimeout(600000).build();
    // 设置请求和传输超时时间
    request.setConfig(requestConfig);
    CloseableHttpResponse response = httpclient.execute(request);
    url = URLDecoder.decode(url, "UTF-8");
    log.info("请求接口:" + url + ",执行状态:" + response.getStatusLine().getStatusCode());
    /** 请求发送成功,并得到响应 **/
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    try {
    /** 读取服务器返回过来的字符串数据 **/
    responseData = EntityUtils.toString(response.getEntity(), "UTF-8");
    } catch (Exception e) {
    log.error("post请求提交失败:" + url, e);
    } finally {
    response.close();
    }
    } else {
    responseData = response.getStatusLine().getStatusCode() + "-" + response.getStatusLine().getReasonPhrase();
    }
    } catch (Exception e) {
    log.error("post请求提交失败:" + url, e);
    } finally {
    try {
    httpclient.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    Timestamp responseTime = new Timestamp(System.currentTimeMillis());
    log.debug("第三方接口-响应时间:" + sdf.format(responseTime));
    log.debug("第三方接口-响应报文:" + responseData);
    return responseData;


    }

  • 相关阅读:
    筛选法求素数
    C/C++经典面试题目
    操作系统笔试面试基本内容
    Win32/MFC的基本概念
    STL采用的标准模板库
    数据结构基本概念
    SQL基础语句
    C/C++基础概念
    计算机网络基础概念
    流水作业 批作业调度
  • 原文地址:https://www.cnblogs.com/lingl/p/11090181.html
Copyright © 2011-2022 走看看