java https连接以及乱码问题解决办法

    用如下命令导入证书文件到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

jdk密钥库的默认密码为changeit

HttpsURLConnection 中文乱码问题

public class httpUTIL {
   private String url;
   private URL myurl;
   private boolean is_ok;
  // private
   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("contentType", "GBK"); 
		https.setRequestProperty("Charset", "utf-8");
		if(https.getResponseCode()==https.HTTP_OK){
			is_ok=true;
		}
		else
			is_ok=false;
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.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 http=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"));
		//编码问题,这边处理一下就ok了,不错 啊啊啊,对流进行转码
		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("到达车站");
		int b=testhtml.indexOf("html");
		//怎么会这样,我的这个中文显示乱码,并且找不到这个中文字符
		System.out.println("杭州"+a+"  "+b);
		String str="test+杭州"+"test";
		int c=str.indexOf("杭州");
		System.out.println(c);
		if(httpscon != null){
			httpscon.disconnect();
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
   }
   
}


java https连接以及乱码问题解决办法,布布扣,bubuko.com

java https连接以及乱码问题解决办法

上一篇:Lesson 3


下一篇:SiR-PEG3-TCO硅基罗丹明peg功能化染料反式环辛烯SiR-TCO