JAVA Access Https__java

Source: Internet
Author: User
Tags static class

Import java.io.*;
Import java.net.*;
Import java.security.cert.CertificateException;
Import Java.security.cert.X509Certificate;
Import javax.net.ssl.*;

public class Trustssl {
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[] {};
}
}

private static class Trustanyhostnameverifier implements Hostnameverifier {
public boolean verify (String hostname, sslsession session) {
return true;
}
}

public static void Main (string[] args) throws Exception {
InputStream in = null;
OutputStream out = null;
byte[] buffer = new byte[4096];
String Str_return = "";
try {
Sslcontext sc = sslcontext.getinstance ("SSL");
Sc.init (NULL, new trustmanager[] {new Trustanytrustmanager ()},
New Java.security.SecureRandom ());
URL console = new URL (
"Https://192.168.1.188/test.php?username= test");
Httpsurlconnection conn = (httpsurlconnection) Console
. OpenConnection ();
Conn.setsslsocketfactory (Sc.getsocketfactory ());
Conn.sethostnameverifier (New Trustanyhostnameverifier ());
Conn.connect ();
InputStream is = Conn.getinputstream ();
DataInputStream indata = new DataInputStream (IS);
String ret = "";

while (ret!= null) {
ret = Indata.readline ();
if (ret!= null &&!ret.trim (). Equals ("")) {
Str_return = Str_return
+ New String (Ret.getbytes ("iso-8859-1"), "GBK");
}
}
Conn.disconnect ();
catch (Connectexception e) {
System.out.println ("Connectexception");
System.out.println (e);
Throw e;

catch (IOException e) {
System.out.println ("IOException");
System.out.println (e);
Throw e;

  } finally {
    try {
     in.close ();
     catch (Exception e) {
   }
    try {
     Out.clos E ();
   } catch (Exception e) {
   }
  }
   System.out.pr Intln (Str_return);
}
}

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.