zoukankan      html  css  js  c++  java
  • http接口的调用

    1.按照文档先写入参数,这里主要介绍

    Json格式的String字符串,包括拼接数组

    String sqr_arry [] = new String[rowList.size()];
    for(int i = 0; i < rowList.size(); i++) {
    FieldList field_p = rowList.get(i);//查询每个家庭成员的姓名和身份证
    String xm = field_p.get("pxm");
    String sfzh = field_p.get("pzjhm");
    String sq = "{"xm":""+xm+"","sfzh":""+sfzh+""}";
    sqr_arry [i] = sq;//把各个家庭对象放进数组
    }
    String sqrs = "";
    for(int i = 0;i < rowList.size(); i++ ){
    sqrs += sqr_arry [i]+",";//从数组中取对象并做拼接
    }
    int idx = sqrs.lastIndexOf(",");//去掉最后一个逗号
    sqrs = sqrs.substring(0,idx);

    String sqr = "["+sqrs+"]";
    String pararsa="{"jkbm":"11","batchid":""+ID+"","sqrs":"+sqr+","remark":""+cxyy+""}";//sqr为数组,解析完外层必须不带双引号“”;

    String urlPost = "http://IP地址:端口号/***/***/***.action"; //接口地址

    String resValue = HttpPost(urlPost, pararsa);//请求接口

    /**
    * 模拟HttpPost请求
    * @param url
    * @param jsonString
    * @return
    */
    public static String HttpPost(String url, String jsonString){
    CloseableHttpResponse response = null;
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();//创建CloseableHttpClient
    HttpPost httpPost = new HttpPost(url);//实现HttpPost
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();
    httpPost.setConfig(requestConfig); //设置httpPost的状态参数
    httpPost.addHeader("Content-Type", "application/json");//设置httpPost的请求头中的MIME类型为json
    StringEntity requestEntity = new StringEntity(jsonString, "utf-8");
    httpPost.setEntity(requestEntity);//设置请求体
    try {
    response = httpClient.execute(httpPost, new BasicHttpContext());//执行请求返回结果
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    return null;
    }
    HttpEntity entity = response.getEntity();
    if (entity != null) {
    String resultStr = EntityUtils.toString(entity, "utf-8");
    return resultStr;
    } else {
    return null;
    }
    } catch (Exception e) {
    logger.error("httpPost method exception handle-- > " + e);
    return null;
    } finally {
    if (response != null){
    try {
    response.close();//最后关闭response
    } catch (IOException e) {
    logger.error("httpPost method IOException handle -- > " + e);
    }
    }if(httpClient != null){try {httpClient.close();} catch (IOException e) {logger.error("httpPost method exception handle-- > " + e);}}}}

    /**
    * 模拟HttpGet 请求
    * @param url
    * @return
    */
    public static String HttpGet(String url){
    //单位毫秒
    RequestConfig requestConfig = RequestConfig.custom()
    .setConnectionRequestTimeout(3000).setConnectTimeout(3000)
    .setSocketTimeout(3000).build();//设置请求的状态参数

    CloseableHttpClient httpclient = HttpClients.createDefault();//创建 CloseableHttpClient
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);

    CloseableHttpResponse response = null;
    try {
    response = httpclient.execute(httpGet);//返回请求执行结果
    int statusCode = response.getStatusLine().getStatusCode();//获取返回的状态值
    if (statusCode != HttpStatus.SC_OK) {
    return null;
    } else {
    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
    return result;
    }
    } catch (Exception e) {
    logger.error("httpget Exception handle-- > " + e);
    } finally {
    if (response != null){
    try {
    response.close();//关闭response
    } catch (IOException e) {
    logger.error("httpget IOException handle-- > " + e);
    }
    }
    if(httpclient != null){
    try {
    httpclient.close();//关闭httpclient
    } catch (IOException e) {
    logger.error("httpget IOException handle-- > " + e);
    }
    }
    }
    return null;
    }

  • 相关阅读:
    php分享三十:php版本选择
    php分享二十九:命名空间
    高性能mysql读书笔记(一):Schema与数据类型优化
    php分享二十八:mysql运行中的问题排查
    php分享二十七:批量插入mysql
    php分享二十六:读写日志
    Python | 一行命令生成动态二维码
    Python-获取法定节假日
    GoLang-字符串
    基础知识
  • 原文地址:https://www.cnblogs.com/tongcc/p/11708974.html
Copyright © 2011-2022 走看看