• 请求第三方https接口


    1. 生成证书
      根据第三方提供的.cer证书,我们可以用JDK的keytool工具生成keystore密钥库文件
      a. keytool -import -v -file E:ceresb.*****-p.com.cn.cer -keystore F:tomcat.keystor

      b. 输入密码(自己给的密码)

      c.回车提示是否信任此证书(y信任)

    2. 代码请求https接口

      public static String httpsPost(String url, String json) {
      InputStream inputStream = null;
      String jsonString = "";
      try {
      DefaultHttpClient client = new DefaultHttpClient();
      CloseableHttpClient httpClient = HttpClients.createDefault();
      client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
      client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
      " Mozilla/5.0 (Windows NT 6.2; rv:18.0) Gecko/20100101 Firefox/18.0");

       	// 获得密匙库
       	KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
       	ClassPathResource resource = new ClassPathResource("tomcat.keystore");
      
       	// 获取输入流
       	inputStream = resource.getInputStream();
      
       	// 密匙库的密码
       	trustStore.load(inputStream, "123456".toCharArray());
      
       	// 注册密匙库
       	SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
      
       	// 不校验域名
       	socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
       	Scheme sch = new Scheme("https", 443, socketFactory);
       	client.getConnectionManager().getSchemeRegistry().register(sch);
      
       	HttpPost httppost = new HttpPost(url);
       	StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
       	httppost.setEntity(entity);
      
       	// 发起请求
       	HttpResponse response = client.execute(httppost);
      
       	// 获取响应
       	HttpEntity resEntity = response.getEntity();
       	jsonString = EntityUtils.toString(resEntity, "utf-8");
       } catch (Exception e) {
       	e.printStackTrace();
       } finally {
       	try {
       		inputStream.close();
       	} catch (IOException e) {
       		e.printStackTrace();
       	}
       }
       return jsonString;
      

      }

  • 相关阅读:
    【转载】搜索题目推荐
    HDU 4629 Burning 几何 + 扫描线
    HDU 4630 No Pain No Game 树状数组+离线查询
    SPOJ 416 Divisibility by 15 细节题
    【转载】树状数组题目
    SPOJ 274 Johnny and the Watermelon Plantation(TLE)
    SPOJ 227 Ordering the Soldiers 线段树 / 树状数组
    HDU 4620 Fruit Ninja Extreme 搜索
    Java序列化与反序列化
    Java IO包装流如何关闭?
  • 原文地址:https://www.cnblogs.com/fanshu/p/10189351.html
Copyright © 2020-2023  润新知