java https连接失败_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();

}

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值