开发者社区> 问答> 正文

linux下钉钉接口调用失败

同样的代码windows下面没有问题,发布到linux下时,获取accesstoken失败 java.io.IOException: HTTPS hostname wrong:  should be <oapi.dingtalk.com>


求解!!!
发起http请求的类,错误抛出在红色标记:

    public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr)
                                                                                                   throws Exception {
        JSONObject jsonObject = null;
        StringBuffer buffer = new StringBuffer();
        // 创建SSLContext对象,并使用我们指定的信任管理器初始化
        TrustManager[] tm = { new MyX509TrustManager() };
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        // 从上述SSLContext对象中得到SSLSocketFactory对象
        SSLSocketFactory ssf = sslContext.getSocketFactory();
        //设置SSL客户端采用TLSv1协议
        System.setProperty("https.protocols", "TLSv1");
        URL url = new URL(requestUrl);
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
        httpUrlConn.setSSLSocketFactory(ssf);


        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setUseCaches(false);
        // 设置请求方式(GET/POST)
        httpUrlConn.setRequestMethod(requestMethod);
        httpUrlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8");


        if ("GET".equalsIgnoreCase(requestMethod))
             httpUrlConn.connect();


        // 当有数据需要提交时
        if (null != outputStr) {
            OutputStream outputStream = httpUrlConn.getOutputStream();
            // 注意编码格式,防止中文乱码
            outputStream.write(outputStr.getBytes("UTF-8"));
            outputStream.close();
        }


        // 将返回的输入流转换成字符串
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        // 释放资源
        inputStream.close();
        inputStream = null;
        httpUrlConn.disconnect();
        jsonObject = JSONObject.fromObject(buffer.toString());


        return jsonObject;
    }

展开
收起
don_shen 2016-01-07 10:58:33 8973 0
2 条回答
写回答
取消 提交回答
  • Relinux下钉钉接口调用失败
    解决了就好,谢谢您的关注
    2016-01-07 22:17:15
    赞同 展开评论 打赏
  • Relinux下钉钉接口调用失败
    问题解决了,在httpUrlConn中设置验证永远通过就可以了,至于为什么windows下可用,linux下必须加,这个问题有待研究

    //验证hostname直接返回true
    httpUrlConn.setHostnameVerifier(TrustAnyHostnameVerifier.getInstance());

    验证类:
    public class TrustAnyHostnameVerifier implements HostnameVerifier {

        private static TrustAnyHostnameVerifier trustAnyHostnameVerifier;

        public static TrustAnyHostnameVerifier getInstance() {
            if (trustAnyHostnameVerifier == null) {
                trustAnyHostnameVerifier = new TrustAnyHostnameVerifier();
            }
            return trustAnyHostnameVerifier;
        }

        public boolean verify(String hostname, SSLSession session) {
            // 直接返回true
            return true;
        }
    }
    2016-01-07 12:01:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载