基于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. StringurlResouce="https://localhost:8443/cloud-storeage-boss/cai:images:public/11汉字.jpg";//createURL
  6. FilelocalFile=newFile("C:\\Users\\Public\\Pictures\\SamplePictures\\Penguins.jpg");
  7. try{
  8. HttpsURLConnectionurlConnection=
  9. (HttpsURLConnection)(newURL(urlResouce)).openConnection();
  10. urlConnection.setRequestProperty("Charset","UTF-8");
  11. urlConnection.setDoInput(true);
  12. urlConnection.setDoOutput(true);
  13. urlConnection.setRequestMethod("PUT");
  14. OutputStreamurlOutputStream=urlConnection.getOutputStream();
  15. FileInputStreamfileInputStream=newFileInputStream(localFile);
  16. IOUtils.copy(fileInputStream,urlOutputStream);
  17. fileInputStream.close();
  18. urlOutputStream.close();
  19. System.out.println(urlConnection.getResponseCode());
  20. }
  21. catch(Exceptione)
  22. {
  23. e.printStackTrace();
  24. }

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

你可能感兴趣的:(urlconnection)