首页 > 其他分享 >CloseableHttpClient设置超时时间demo 未设置默认是2分钟

CloseableHttpClient设置超时时间demo 未设置默认是2分钟

时间:2022-08-15 13:56:09浏览次数:72  
标签:http demo CloseableHttpClient obj 设置 org apache import post

# CloseableHttpClient设置超时时间demo 未设置默认是2分钟

import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

# org.apache.httpcomponents:httpclient:4.5.5

# 方法执行代码
            String content = this.toJson(map or object对象转json);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            
            StringEntity stringEntity = new StringEntity(content);
            HttpPost post = new HttpPost(callbackUrlPic);
            post.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            post.setEntity(stringEntity);
            //设置超时时间,未设置默认是2分钟
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(6000).build();
            post.setConfig(requestConfig);

            CloseableHttpResponse response = httpClient.execute(post);
            String res = EntityUtils.toString(response.getEntity());
            //返回值的处理 todo


public static String toJson(Object obj) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            logger.error("convert obj to json error. obj="+obj, e);
        }
        return null;
    }
    

 

标签:http,demo,CloseableHttpClient,obj,设置,org,apache,import,post
From: https://www.cnblogs.com/oktokeep/p/16588058.html

相关文章

  • 设置rem
    rem(根元素的字体大小)<script> functionauto(){              /获取当前屏幕宽度/              vardevice......
  • CAD设置经典模式
    1、打开桌面CAD2020软件,点击开始绘制。2、点击最上面的倒三角,下拉,点击【显示菜单栏】; 3、点击菜单栏的【工具】,点击【选项板】,点击【功能区】,关闭功能区;  4、......
  • Nodemon 如何实时监听 TypeScript 项目下的文件并热部署?
    首先你的项目要安装ts-node和nodemon:npmi-Dts-nodenodemon在package.json文件中配置运行脚本:"dev":"nodemon--watchsrc/**/*.ts--exec\\\"ts-node\\\"src/ma......
  • demo
    //定义的学生类publicclassStudent{ privateStringname; privateStringage; privateStringid; privateStringaddress; publicStudent(){ } publi......
  • Django设置跨域访问
    Django设置跨域访问(1)安装DjangoCorsHeaderspipinstalldjango-cors-headers(2)settings.py配置如下INSTALLED_APPS=[#'django.contrib.admin','d......
  • Qt6.2 Widgets Application 设置应用图标及找不到.pro文件的问题
    参考【Qt】设置应用程序图标数以千计的免费图标环境环境版本windows10QT6.2.4QtCreator8.0.1(Community)qmake步骤qt6.2.4创建默认......
  • nodemon 调试nodeJS的使用方法
    首先你需要全局安装nodemon ,然后要做一个launch.json的配置:{"version":"0.2.0","configurations":[{"name":"Launchserver.js......
  • 12.Matplotlib grid()设置网格格式
    通过Matplotlibaxes对象提供的grid()方法可以开启或者关闭画布中的网格(即是否显示网格)以及网格的主/次刻度。除此之外,grid()函数还可以设置网格的颜色、线型以及线......