Java send HTTPS request, certificate configuration

Source: Internet
Author: User
Tags tomcat stringbuffer

With Certificate:

Getting Tomcat SSL (HTTPS) Working
1.Create a certificate keystore containing a single self-signed certificate by executing the Follo   Wing command.   Specify a password value of "Changeit". Note, this command creates both the certificate and the KeyStore

Windows: Enter in the%java_home%/bin/directory:

Keytool-genkey-alias Tomcat-keyalg Rsa-keystore Tomcat.keystore

Generate a Tomcat.keystore file
Unix: $JAVA _home/bin/Input: Keytool-genkey-alias tomcat-keyalg rsa-keystore Tomcat.keystore

Enter in D:/tomcat 5.0/conf directory: Keytool-genkey-alias tomcat-keyalg rsa-keystore Tomcat.keystore
Enter KeyStore Password: Changeit
What is your first and last name?
[Unknown]: localhost
What is the name of your organizational unit?
[Unknown]: the

What is the name of your organization.
[Unknown]: Hell
What is the name of your city or region.
[Unknown]: Hangzhou
What is the name of your state or province.
[Unknown]: Zhejiang

What is the two-letter country code for this unit
    [Unknown]:     CH
cn=localhost,   ou=wict,   O=hell,   l= Wuhan,   St=hubei,   c=ch   right.
    [No]:     y

Enter <tomcat> main password
                (if the same as   keystore   password, press ENTER): Here I pressed the carriage return
2.     Copy   The   keystore   file &nb Sp To   catalina_home/conf   copy to Tomcat's Conf
3.     Uncomment   the   "SSL   http/ 1.1   Connector "  entry   in   $CATALINA _home/conf/server.xml.   Your   Entry   should   look   like:
<!--  Define   a   SSL   Coyot E   http/1.1   Connector   on   port   8443  -->
<!---->

<connector port= "8443" maxhttpheadersize= "8192"
                maxthreads= "minsparethreads=" maxsparethreads= "a" of "a"                 enablelookups= "false" disableuploadtimeout= " True "
               acceptcount=" 100 " Scheme= "https" secure= "true"
                Clientauth= "false" sslprotocol= "TLS"
                keystorefile= "/conf/tomcat.keystore"
             truststorefile= "/conf/tomcat.keystore"/>

Save after adding, browse for the following address to visit Tomcat home https://localhost:8443
4. Copy the KeyStore file to the default file location of the Java applications, then run The Java application. Copy to the engineering directory, note that the direct re-engineering below the level of the directory.

5, Java code:

public static void Main (string[] args) throws Exception {
Serverkeys is your own certificate generated by Keytool
System.setproperty ("Javax.net.ssl.trustStore", "Tomcat.keystore");
System.setproperty ("Javax.net.ssl.trustStorePassword", "Changeit");
Connect to HTTPS https://www.sun.com
URL url = new URL ("https://localhost:8443");
HttpURLConnection connection = (httpurlconnection) url.openconnection ();
Connection.setrequestmethod ("POST");
Connection.setdooutput (TRUE);
Connection.setdoinput (TRUE);
System.out.println ("1--conected to" + connection.tostring ());

DataOutputStream dos = new DataOutputStream (Connection.getoutputstream ());
Byte[] rr = new byte[dos.size ()];
Dos.write (rr,0,rr.length);
Dos.flush ();
string ss = new string (RR);
System.out.println ("2-----" +ss);

StringBuffer Outbuff = new StringBuffer ();
BufferedReader in = new BufferedReader (New InputStreamReader (Connection.getinputstream ()));
String Line;
while (line = In.readline ())!= null) {
Outbuff.append (line);
}
In.close ();
System.out.println ("3---Test:" + outbuff.tostring ());
}

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.