http接口访问

整体的接口访问代码

/**
* post
*  @param url  访问接口
* @param jsonParam  接口参数
* @return
*/
public static String httpPost(String url, String jsonParam) {
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost method = new HttpPost(url);
        try {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(CONNECTTIMEOUT).build();
            method.setConfig(requestConfig);
            method.setHeader("Accept-Encoding", "gzip,deflate");
            if (null != jsonParam) {
                StringEntity entity = new StringEntity(jsonParam,"utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            HttpResponse result = httpClient.execute(method);
            url = URLDecoder.decode(url, "UTF-8");
            String str = getDataString(url, result);
            if (str != null) return str;
        } catch (IOException e) {
            logger.error("" + url, e);
        }
        return null;
    }

其中设置超时等待时间方式为这里设置的是60000ms的等待时间
setConnectTimeout 是建立连接的超时时间
setSocketTimeout是建立连接的超时时间

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(CONNECTTIMEOUT).build();
method.setConfig(requestConfig);

猜你喜欢

转载自blog.csdn.net/lazy_zzzzzz/article/details/79986880
今日推荐