首页 > 编程语言 > 详细

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

时间:2014-04-04 01:30:06      阅读:498      评论:0      收藏:0      [点我收藏+]

    用如下命令导入证书文件到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连接以及乱码问题解决办法

原文:http://blog.csdn.net/sn_zzy/article/details/22874501

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!