假如您需要通过js跨域向https发起请求,方法如下两种:

假如您需要通过js跨域向https发起请求,方法如下两种:

第一种:$.getJSON 后面的url中需要加上?callback=?

注意:被访问的https服务器端必须要返回json数据。

如果您服务器不能改动,只能我们在客户端想办法的话,用下面第二种方法吧


第二种:

private static class TrustAnyTrustManager implements X509TrustManager {

   public void checkClientTrusted(X509Certificate[] chain, String authType)
     throws CertificateException {
   }

   public void checkServerTrusted(X509Certificate[] chain, String authType)
     throws CertificateException {
   }

   public X509Certificate[] getAcceptedIssuers() {
    return new X509Certificate[] {};
   }
}

@SuppressWarnings("deprecation")
private static class TrustAnyHostnameVerifier implements com.sun.net.ssl.HostnameVerifier {
@Override
public boolean verify(String arg0, String arg1) {
      return true;
}
}


public void activeI2000KeepLive(){
   InputStream is = null;
   PrintWriter pw = null;
   try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
      new java.security.SecureRandom());
    String remoteUrl = InitAction.REMOTE_URL;
    final String url = remoteUrl.substring(0,remoteUrl.indexOf("/", 8)) + "/servlet/keeplive";
    URL console = new URL(url);
//    javax.net.ssl.HttpsURLConnection conn =  (HttpsURLConnection) console.openConnection();
    com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl conn =  (HttpsURLConnectionOldImpl) console.openConnection();
    conn.setSSLSocketFactory(sc.getSocketFactory());
    conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
    conn.connect();
    is = conn.getInputStream();
    DataInputStream indata = new DataInputStream(is);
    String ret = "";
    String str_return = "";
    while (ret != null) {
     ret = indata.readLine();
     if (ret != null && !ret.trim().equals("")) {
      str_return = str_return
        + new String(ret.getBytes("ISO-8859-1"), "GBK");
     }
    }
    System.out.println(str_return);
    
    pw = ServletActionContext.getResponse().getWriter();
    if(is != null){
        pw.write("success");
    }else{
        pw.write("error");
    }
    
    conn.disconnect();
   } catch (ConnectException e) {
       
   }catch (Exception e) {
       
   }
   finally {
     try {
         if(is != null){
             is.close();
         }
         if(pw != null){
             pw.close();
         }
    } catch (IOException e) {
    }
   }
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值