http远程请求失败,绕过安全协议进行请求

最近有个项目是接入远程接口获取数据进行处理,结果使用javahttp一直失败,经查是安全协议证书的问题,试过了多种方法仍然失败,最后直接绕过安全协议,完美解决问题。

/**
     * @param url   需要请求的网关路径
     * @param sendData  请求时需要传入的参数
     * @param urlencode url的编码格式
     * @param connTimeOut   链接超时时间 
     * @param readTimeOut   读取超时时间
     * @param contentType   请求头部  固定输入"application/x-www-form-urlencoded;charset="+urlencode
     * @param header     输入null
     * @return
     */
    public static String sendAndRcvHttpPostBase(String url,String sendData,String urlencode,int connTimeOut,int readTimeOut,String contentType,Map<String,String> header){
        Long curTime = System.currentTimeMillis();
        String result = "";
        BufferedReader in = null;
        DataOutputStream out = null;
        int code = 999;
        HttpsURLConnection httpsConn = null;
        HttpURLConnection httpConn = null;
        try{
            URL myURL = new URL(url);
            if(url.startsWith("https://")){
                httpsConn =    (HttpsURLConnection) myURL.openConnection();
                TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
                            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                return null;
                            }
                            public void checkClientTrusted(
                                java.security.cert.X509Certificate[] certs, String authType) {
                            }
                            public void checkServerTrusted(
                                java.security.cert.X509Certificate[] certs, String authType) {
                            }
                        }
                    };
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                httpsConn.setSSLSocketFactory(sc.getSocketFactory());
                HostnameVerifier hv = new HostnameVerifier() {
                    public boolean verify(String urlHostName, SSLSession session) {
                        return true;
                    }
                }; 
                httpsConn.setHostnameVerifier(hv);
                    
                httpsConn.setRequestProperty("Accept-Charset", urlencode);
                httpsConn.setRequestProperty("User-Agent","java HttpsURLConnection");
                if(header!=null){
                    for(String key:header.keySet()){
                        httpsConn.setRequestProperty(key, (String)header.get(key));
                    }
                }
                httpsConn.setRequestMethod("POST");
                httpsConn.setUseCaches(false);
                httpsConn.setRequestProperty("Content-Type",contentType); 
                httpsConn.setConnectTimeout(connTimeOut);
                httpsConn.setReadTimeout(readTimeOut);
                httpsConn.setDoInput(true);
                httpsConn.setInstanceFollowRedirects(true); 
                if(sendData !=null){
                    httpsConn.setDoOutput(true);
                    // 获取URLConnection对象对应的输出流
                    out = new DataOutputStream(httpsConn.getOutputStream());
                    // 发送请求参数
                    out.write(sendData.getBytes(urlencode));
                    // flush输出流的缓冲
                    out.flush();
                    out.close();
                }
                // 取得该连接的输入流,以读取响应内容
                in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream(),urlencode));
                code = httpsConn.getResponseCode();
            }else{
                httpConn =    (HttpURLConnection) myURL.openConnection();
                httpConn.setRequestProperty("Accept-Charset", urlencode);
                httpConn.setRequestProperty("user-agent","java HttpURLConnection");
                if(header!=null){
                    for(String key:header.keySet()){
                        httpConn.setRequestProperty(key, (String)header.get(key));
                    }
                }
                httpConn.setRequestMethod("POST");
                httpConn.setUseCaches(false);
                httpConn.setRequestProperty("Content-Type",contentType); 
                httpConn.setConnectTimeout(connTimeOut);
                httpConn.setReadTimeout(readTimeOut);
                httpConn.setDoInput(true);
                httpConn.setInstanceFollowRedirects(true); 
                if(sendData !=null){
                    httpConn.setDoOutput(true);
                    // 获取URLConnection对象对应的输出流
                    out = new DataOutputStream(httpConn.getOutputStream());
                    // 发送请求参数
                    out.write(sendData.getBytes(urlencode));
                    // flush输出流的缓冲
                    out.flush();
                    out.close();
                }
                // 取得该连接的输入流,以读取响应内容
                in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),urlencode));
                code = httpConn.getResponseCode();
            }
            if (HttpURLConnection.HTTP_OK == code){ 
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                    
                    System.out.println("=====反回结果====="+ line);
                    
                    
                }
                if(result.length()>2000){
                }else{
                }
            }else{
                result = null;
                throw new Exception("支付失败,服务端响应码:"+code);
            }
        }catch(IOException e){
            result = null;
        }catch(Exception e){
            result = null;
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
            if(httpConn!=null){
                httpConn.disconnect();
            }
            if(httpsConn!=null){
                httpsConn.disconnect();
            }
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return result;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值