Solutions to java https connections and garbled characters

Source: Internet
Author: User

Run the following command to import the Certificate file to jdk:

Keytool-import-alias 12306-keystore cacerts-file D: \ srca12306 \ srca. cer-trustcacerts
Keytool-import-keystore % JAVA_HOME % \ jre \ lib \ security \ cacerts-file D: \ srca12306 \ srca. cer-alias 12307
Keytool-import-keystore cacerts-file D: \ srca12306 \ srca. cer-alias 12307

The default jdk keystore password is changeit.

HttpsURLConnection Chinese garbled Problem

Public class httpUTIL {private String url; private URL myurl; private boolean is_ OK; // private HttpsURLConnection https; public httpUTIL (String url) {this. url = url; https = null;} public void init () throws MalformedURLException {myurl = new URL (url); try {https = (HttpsURLConnection) myurl. openConnection (); https. setRequestProperty ("Accept-Language", "zh-CN"); // https. setRequestProperty ("content Type "," GBK "); https. setRequestProperty ("Charset", "UTF-8"); if (https. getResponseCode () = https. HTTP_ OK) {is_ OK = true;} elseis_ OK = false;} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public HttpsURLConnection gethttps () {return https;} public boolean gethttp_status () {return is_ OK;} public static void main (String args []) {String url = bookInfo. book_init; httpUTIL htt P = new httpUTIL (url); HttpsURLConnection httpscon; StringBuilder testhtml = new StringBuilder (""); InputStream in; BufferedReader buffread; try {http. init (); httpscon = http. gethttps (); if (httpscon = null) {System. out. println ("https null"); return;} httpscon. connect (); in = httpscon. getInputStream (); if (in = null) {System. out. println ("in null"); return;} buffread = new BufferedReader (new InputStreamReader (in, "UTF-8 "); // Encoding problem. It will be okay to handle it here. It's good. The stream is transcoded String line; line = buffread. readLine (); while (line! = Null) {testhtml. append (line); line = buffread. readLine ();} // System. out. println (testhtml); String strtest = testhtml. toString (); System. out. println (strtest); int a = strtest. indexOf ("arriving at Station"); int B = testhtml. indexOf ("html"); // how can this problem be solved? garbled characters are displayed in Chinese and the Chinese character System cannot be found. out. println ("Hangzhou" + a + "" + B); String str = "test + Hangzhou" + "test"; int c = str. indexOf ("Hangzhou"); System. out. println (c); if (httpscon! = Null) {httpscon. disconnect () ;}} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}


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.