If you need to request via JS to https across domains, here are two ways: __js

Source: Internet
Author: User
Tags static class

If you need to request via JS to https across domains, here are two ways:

The first one: $.getjson should be added in the URL behind the callback=?

Note: The HTTPS server that is being accessed must return JSON data.

If you can not change the server, we can do it on the client, use the following second method


The second type:

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) {
}
}
}









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.