• Http post请求案例




    public
    RmiRespBase sendHttpRes(String jsonParamStr, String url, String apiName,String systemId,String apiUrl) { sendLog(apiName,url + apiUrl,jsonParamStr,null); //自定义日志方法

          //设置请求头headers HttpHeaders headers
    = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        //将headers和参数 json放入entity对象中 HttpEntity
    <String> entity = new HttpEntity<>(jsonParamStr, headers); //设置连接超时时间 SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(Integer.valueOf(timeoutvalue)); requestFactory.setReadTimeout(Integer.valueOf(timeoutvalue));
          //创建远程调用api,并构造连接超时的设置 restTemplate
    = new RestTemplate(requestFactory);       //调用,并返回 JSONObject body = restTemplate.postForEntity(url + apiUrl, entity, JSONObject.class).getBody();

          //自定义逻辑,返回结果 RmiRespBase respBase
    = new RmiRespBase(url + apiUrl, apiName, systemId, body); sendLog(apiName,url + apiUrl,jsonParamStr,body.toJSONString()); //自定义日志方法 return respBase; }
    /**
        * 描述:
        *   基础Http调用
        * @Param [jsonParamStr, url, apiName, systemId, apiUrl]
        **/
        public RmiRespBase sendHttpRes(String jsonParamStr, String url, String apiName,String systemId,String apiUrl) {
            sendLog(apiName,url + apiUrl,jsonParamStr,null);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity<String> entity = new HttpEntity<>(jsonParamStr, headers);
    
            //设置连接超时时间
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(Integer.valueOf(timeoutvalue));
            requestFactory.setReadTimeout(Integer.valueOf(timeoutvalue));
            restTemplate = new RestTemplate(requestFactory);
    
            JSONObject body = restTemplate.postForEntity(url + apiUrl, entity, JSONObject.class).getBody();
            RmiRespBase respBase = new RmiRespBase(url + apiUrl, apiName, systemId, body);
            sendLog(apiName,url + apiUrl,jsonParamStr,body.toJSONString());
            return respBase;
        }
  • 相关阅读:
    Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法
    解决 vim 报错:the imp module is deprecated in favour of importlib
    SIFT了解,哪些方法可以在现在的AI算法中借鉴?
    CLAHE
    实际算法项目工程上手日志C/C++
    OS X 切换gcc版本
    opencv3.4.2 cmake错误:in-source builds are not allowed
    C++ opencv 计算两张图像的PSNR相似度
    如何在OS X 中使用markdown + latex混合记笔记?
    给anaconda 换源
  • 原文地址:https://www.cnblogs.com/mh-study/p/11840583.html
Copyright © 2020-2023  润新知