啥也不会,就会吃睡 2022-04-08 14:18 采纳率: 0%
浏览 218
已结题

client.execute()执行超时后也没有返回值,偶发

问题遇到的现象和发生背景

client.execute(method, context)执行超时后也没有返回值,入参打印后等超时时间(6秒)后下面的日志也没打印,方法最后返回了null

问题相关代码,请勿粘贴截图
public static String httpRequestToString(String url, String requestMethod,
        Map<String, String> params, String lx,String ...auth){
    //接口返回结果
    String methodResult = null;
    try {
        String parameters = "";
        boolean hasParams = false;
        //将参数集合拼接成特定格式,如name=zhangsan&age=24
        for(String key : params.keySet()){
            String value = URLEncoder.encode(params.get(key), "UTF-8");
            parameters += key +"="+ value +"&";
            hasParams = true;
        }
        if(hasParams){
            parameters = parameters.substring(0, parameters.length()-1);
        }
        //是否为GET方式请求
        boolean isGet = "get".equalsIgnoreCase(requestMethod);
        boolean isPost = "post".equalsIgnoreCase(requestMethod);
        boolean isPut = "put".equalsIgnoreCase(requestMethod);
        boolean isDelete = "delete".equalsIgnoreCase(requestMethod);

        //创建HttpClient连接对象
        DefaultHttpClient client = new DefaultHttpClient();
        HttpRequestBase method = null;
        if(isGet){
            url += "?" + parameters;
            method = new HttpGet(url);
        }else if(isPost){
            method = new HttpPost(url);
            HttpPost postMethod = (HttpPost) method;
            StringEntity entity = new StringEntity(parameters);
            postMethod.setEntity(entity);
        }else if(isPut){
            method = new HttpPut(url);
            HttpPut putMethod = (HttpPut) method;
            StringEntity entity = new StringEntity(parameters);
            putMethod.setEntity(entity);
        }else if(isDelete){
            url += "?" + parameters;
            method = new HttpDelete(url);
        }
        method.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
        //设置参数内容类型
        method.addHeader("Content-Type",lx);
        //httpClient本地上下文
        HttpClientContext context = null;
        if(!(auth==null || auth.length==0)){
            String username = auth[0];
            String password = auth[1];
            UsernamePasswordCredentials credt = new UsernamePasswordCredentials(username,password);
            //凭据提供器
            CredentialsProvider provider = new BasicCredentialsProvider();
            //凭据的匹配范围
            provider.setCredentials(AuthScope.ANY, credt);
            context = HttpClientContext.create();
            context.setCredentialsProvider(provider);
        }
        //访问接口,返回状态码
        log.info("client执行入参method:{},context:{},",method,context);
        HttpResponse response = client.execute(method, context);
        log.info("client执行的返参{},",response);
        //返回状态码200,则访问接口成功
        if(response.getStatusLine().getStatusCode()==200){
            methodResult = EntityUtils.toString(response.getEntity());
        }
        client.close();
    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }
    return methodResult;
}
运行结果及报错内容

方法最后返null,请求后的日志也没打印,偶发的,

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月16日
    • 创建了问题 4月8日

    悬赏问题

    • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
    • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
    • ¥15 clousx6整点报时指令怎么写
    • ¥30 远程帮我安装软件及库文件
    • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
    • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
    • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
    • ¥15 Android studio 无法定位adb是什么问题?
    • ¥15 C#连接不上服务器,
    • ¥15 angular项目错误