Java simulated HTTPS request

Source: Internet
Author: User
A recent app to use cloud storage, compared to a few cloud space, finally chose Bmob (http://www.bmob.cn/), Bmob features a lot, but also provides a variety of APIs, free.

Because my app is small and business is simple, the rest API provided directly with Bmob.

Bmob REST API is HTTPS protocol, so my idea is: Lead out Bmob certificate, use Java Keytool tool to make Certificate library (KeyStore), then use httpsurlconnection and Bmob server communication.

The following steps are specific:

1: Export Certificate
Use the IE Certificate tool to export the certificate of the Web site. Open the Web page, view the page's property page, the property page in the lower-right corner to find the certificate, and finally "copy the certificate to a file."



Because the Java Keytool tool cannot import certificates in the P7B format, in the Import Wizard, I select the BASE64-encoded CER certificate:


Finally get the certificate file:


2: Create the Certificate library and import the certificate
With the Java Keytool Tool, execute the following command to get the Bmob.keystore certificate library file:

--Generate Certificate library files
Keytool-genkey-alias bmob.keystore-keyalg rsa-validity 100000-keystore bmob.keystore

--Import Der/cer Certificate
Keytool-import-file Bmob.cer-keystore Bmob.keystore

Note: When importing a bmob.cer file into a certificate library, it is best to have the same directory as the certificate library

3:java Call Request
Static block Setting Certificate:
/** * * * *
 Load Certificate
 * * * *
/static {
	System.setproperty ("Javax.net.ssl.trustStore", "C:\\bmob.keystore");
	System.setproperty ("Javax.net.ssl.trustStorePassword", "password");
}

Post method:
/** * Add Example * @see <a href= ' Http://docs.bmob.cn/restful/developdoc/index.html?menukey=develop_doc&key=develop_ Restful#index_ Add Data ' > Example </a> */public static void Add () throws Exception {//build request URL PostURL = new URL (add_u

	RL); Httpsurlconnection con = (httpsurlconnection) posturl.openconnection ();//Open Connection Con.setrequestmethod ("POST");
	Post method commits con.setdooutput (TRUE);//Open Read-write properties, default is False Con.setdoinput (true);

	Con.setusecaches (false);//post request cannot use cache con.setinstancefollowredirects (TRUE);
	Add header information Con.setrequestproperty ("X-bmob-application-id", app_id);
	Con.setrequestproperty ("X-bmob-rest-api-key", Api_key);

	Con.setrequestproperty ("Content-type", "Application/json");

	DataOutputStream out = new DataOutputStream (Con.getoutputstream ());
	Send request String data = "{\" name\ ": \" Tom\ "}";
	Out.writebytes (data);
	Out.flush ();

	Out.close ();
	Receive data BufferedReader reader = new BufferedReader (New InputStreamReader (Con.getinputstream (), "UTF-8"));
	String Line; StringbuffEr responsetext = new StringBuffer ();
	while (line = Reader.readline ())!= null) {Responsetext.append (line). Append ("\ r \ n");
	} reader.close ();
	Con.disconnect ();
System.out.println (Responsetext.tostring ()); }

Get Way:
/** * Query Data example * @see <a target=_blank href= "http://docs.bmob.cn/restful/developdoc/index.html?menukey=develop_doc& Amp;key=develop_restful#index_ query Data "> Example </a> * * */public static void Select () throws Exception {//Build request URL Post

	url = new URL (select_url);
	Httpsurlconnection con = (httpsurlconnection) posturl.openconnection ();//Open Connection Con.setrequestmethod ("get");//get method Commit
	Con.setdoinput (TRUE);

	Con.setinstancefollowredirects (TRUE);
	Add header information Con.setrequestproperty ("X-bmob-application-id", app_id);
	Con.setrequestproperty ("X-bmob-rest-api-key", Api_key);

	Con.setrequestproperty ("Content-type", "Application/json");
	Receive data BufferedReader reader = new BufferedReader (New InputStreamReader (Con.getinputstream (), "UTF-8"));
	String Line;
	StringBuffer responsetext = new StringBuffer ();
	while (line = Reader.readline ())!= null) {Responsetext.append (line). Append ("\ r \ n");
	} reader.close ();
	Con.disconnect ();
System.out.println (Responsetext.tostring ()); }

In fact, the same as the HTTP request, but more than the Certificate Production section.

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.