`

基于https,HttpsURLConnection客户端代码调用

 
阅读更多

今天遇到配置了SSL的服务器,不知道怎么写客户端调用,费了一定时间还是折腾通了,mark下与大家共勉

  1. System.setProperty("javax.net.ssl.keyStore","c:\\tomcat.jks");    
  2.             System.setProperty("javax.net.ssl.keyStorePassword","12345678");     
  3.             System.setProperty("javax.net.ssl.trustStore","c:\\tomcat.jks");     
  4.             System.setProperty("javax.net.ssl.trustStorePassword","12345678");     
  5.         String urlResouce = "https://localhost:8443/cloud-storeage-boss/cai:images:public/11汉字.jpg"; // create URL  
  6.         File localFile = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg");  
  7.         try{  
  8.               
  9.         HttpsURLConnection urlConnection =  
  10.         (HttpsURLConnection) (new URL(urlResouce)).openConnection();  
  11.         urlConnection.setRequestProperty("Charset""UTF-8");  
  12.         urlConnection.setDoInput(true);  
  13.         urlConnection.setDoOutput(true);  
  14.         urlConnection.setRequestMethod("PUT");  
  15.         OutputStream urlOutputStream = urlConnection.getOutputStream();  
  16.         FileInputStream fileInputStream = new FileInputStream(localFile);  
  17.         IOUtils.copy(fileInputStream, urlOutputStream);  
  18.         fileInputStream.close();  
  19.         urlOutputStream.close();  
  20.         System.out.println(urlConnection.getResponseCode());  
  21.         }  
  22.         catch(Exception e)  
  23.         {  
  24.             e.printStackTrace();  
  25.         }  


关键就是系统参数的设置,将证书设置到系统中去,如果已经将安全证书安装到JDK那就很easy了

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics