将代码放到服务器上时就会出现证书异常 ,本地测试就没有问题
发布于 6 年前 作者 lei74 5352 次浏览 来自 问答

异常:java.security.cert.CertificateException: No subject alternative DNS name matching api.weixin.qq.com found.

代码如下:

try {

            URL realUrl = new URL(url);

            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

            // 设置为GET方式  

            connection.setRequestMethod(“GET”);  

         // 设置通用的请求属性

            connection.setRequestProperty(“accept”, “*/*”);

            connection.setRequestProperty(“connection”, “Keep-Alive”);

            connection.setRequestProperty(“user-agent”, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)”);

            connection.connect();

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {  

                // 得到响应消息  

                String message = connection.getResponseMessage(); 

                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                String line;

                String responseJson="";

                while ((line = in.readLine()) != null) {

                responseJson += line;

                }

                ObjectMapper mapper = new ObjectMapper();  

                JsonNode rootNode = mapper.readTree(responseJson); // 读取Json  

                openid =rootNode.path(“openid”).asText();

            }  

//            System.out.println(“openid:”+openid);

            return openid;

        }catch (Exception e) {

throw new UserException(“no internet:”+e.getMessage());

}

回到顶部