• Httpclient超时timeout设置


     

    一:连接超时:connectionTimeout

     

         1:指的是连接一个url的连接等待时间。

    二:读取数据超时:soTimeout

         1:指的是连接上一个url,获取response的返回等待时间。

    For example:

     // 设置连接时间
                client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
                client.getHttpConnectionManager().getParams().setSoTimeout(60000);
     /**
         * 消息发送处理.
         * HTTP发送方式
         * @param content 消息内容
         * @return 返回消息
         */
    	private GeneralReturnInfo postHttpData(String content) {
            GeneralReturnInfo out = new GeneralReturnInfo();
            String result = "";
            PostMethod postMethod = new PostMethod(serviceUrl);
            
            try {
                postMethod.setParameter("content", content);
    
                HttpClient client = new HttpClient();
                client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
                // 设置连接时间
                client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
                client.getHttpConnectionManager().getParams().setSoTimeout(60000);
                int status = client.executeMethod(postMethod);
                if (status == HttpStatus.SC_OK) {
                    result = postMethod.getResponseBodyAsString();
                    if (StringUtils.isEmpty(result)) {
                    	String msg="HTTP访问失败(返回报文为空)(" + serviceUrl + ").";
    					System.err.println(msg);
                        throw new AdapterException(ErrorType.CLIENT_NET_ERROR, "", msg);
                    }
                    
                    out = JSON.parseObject(result, GeneralReturnInfo.class);
                } else {
                	String msg="HTTP访问:返回状态不等于200(" + status + ")(" + serviceUrl + ")).";
                    System.err.println(msg);
                    throw new AdapterException(ErrorType.CLIENT_NET_ERROR, "", msg);
                }
    
            } catch (Exception e) {
            	
            	
                // 将新产生的例外封装
                if (e instanceof AdapterException) {
                    throw (AdapterException) e;
                } else if (e instanceof ConnectException) {
                    System.err.println("HTTP访问失败(连接失败)(" + serviceUrl + ")).");
                    throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(连接失败)(" + serviceUrl + ")).");
                } else if (e instanceof ConnectTimeoutException) {
                    System.err.println("HTTP访问失败(连接超时)(" + serviceUrl + ")).");
                    throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(连接超时)(" + serviceUrl + ")).");
                } else if (e instanceof SocketTimeoutException) {
                    System.err.println("HTTP访问失败(访问超时)(" + serviceUrl + ")).");
                    throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(访问超时)(" + serviceUrl + ")).");
                } else {
                    System.err.println("HTTP访问失败(调用异常)(" + serviceUrl + ")).");
                    throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(调用异常)(" + serviceUrl + ")).");
                }
            } finally {
                // 释放连接
                postMethod.releaseConnection();
            }
            return out;
        }
    

     com.creditharmony.apporveadapter.core.client.ClientPoxy    第170行;

     

    www.beicaiduo.com
  • 相关阅读:
    HiLink & LiteOS & IoT芯片 让IoT开发简单高效
    HiCar基本功能介绍
    HiCar技术概述
    docker 笔记 3
    drools规则引擎笔记 与 代码demo
    Spring Reactive Reactor WebFlux Flux Mono 是否能完全取代多线程编程 ?
    webflux demo 1
    阅后归档
    阅后归档
    POJ3252 Round Numbers(数位dp)
  • 原文地址:https://www.cnblogs.com/hoge66/p/8351773.html
Copyright © 2020-2023  润新知