package com.wl.webservice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ricoh.rapp.ezcx.iwbservice.util.Constant;
import com.ricoh.rapp.ezcx.iwbservice.util.Utils; public class HttpsRequest {   private final Logger logger = LoggerFactory.getLogger(HttpsRequest.class); /**
* 处理https GET/POST请求
* @param requestUrl 请求地址
* @param requestMethod 请求方法
* @param requestStr 请求参数
* @return
*/
  public JSONObject httpsRequest(String requestUrl, String requestMethod, String requestStr) {
    logger.info("req---->:" + requestMethod + requestStr);
    HttpsURLConnection httpsConnection = null;
    try {
      // 创建SSLContext
      SSLContext sslContext = SSLContext.getInstance("SSL");
      TrustManager[] tm = { new TrustManager() };
      // 初始化
      sslContext.init(null, tm, new java.security.SecureRandom());       // 获取SSLSocketFactory对象
      SSLSocketFactory ssf = sslContext.getSocketFactory();
      HostnameVerifier HostnameVerifier = new HostnameVerifier() {
        @Override
        public boolean verify(String var1, SSLSession var2) {
          return true;
        }       };       URL url = new URL(requestUrl);
      httpsConnection = (HttpsURLConnection) url.openConnection();
      httpsConnection.setDoOutput(false);
      httpsConnection.setDoInput(true);
      httpsConnection.setConnectTimeout("3 * 1000");
      httpsConnection.setReadTimeout("15 * 1000");
      httpsConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
      httpsConnection.setRequestProperty("Charset", "UTF-8");
      httpsConnection.setRequestProperty("Authorization", "Basic aXdidXNlcjp0ZXN0MDAwMA==");
      httpsConnection.setRequestProperty("User-Agent", "Client identifier");
      httpsConnection.setRequestMethod("GET");
      /*
      * httpsConnection.setUseCaches(false);
      * httpsConnection.setRequestMethod(requestMethod);
      */
      // 设置当前实例使用的SSLSoctetFactory
      httpsConnection.setSSLSocketFactory(ssf);
      httpsConnection.setHostnameVerifier(HostnameVerifier);
      httpsConnection.connect();
      // 往服务器端写内容
      // 读取服务器端返回的内容
      InputStream inputStream = httpsConnection.getInputStream();
      if (httpsConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        logger.error("connect ezcs service failed: " + httpsConnection.getResponseCode());
        JSONObject responseJson = new JSONObject();
        responseJson.put(ResponseKey.KEY_RESULT,
          com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
        return responseJson;
      }
      String response = Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);
      logger.debug("response from ezcs service: " + response);
      JSONObject responseJson = JSON.parseObject(response);
      return responseJson;
    } catch (Exception e) {
      e.printStackTrace();
      logger.debug("connect local ezcs service exception: " + e.getMessage());
      JSONObject responseJson = new JSONObject();
      if (e instanceof SocketTimeoutException || e instanceof SocketException) {
        responseJson.put(ResponseKey.KEY_RESULT,
          com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);
      } else {
        responseJson.put(ResponseKey.KEY_RESULT,
          com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_INNER_ERROR);
      }
     return responseJson;
    } finally {
      if (httpsConnection != null) {
        httpsConnection.disconnect();
      }
    }
  }   class TrustManager implements X509TrustManager {     @Override
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {     }       @Override
    public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {     }     @Override
    public X509Certificate[] getAcceptedIssuers() {
      return new X509Certificate[] {};
    }   }   public static class ResponseKey {
    public static final String KEY_RESULT = "result";
    public static final String KEY_REASON = "reason";
    public static final String KEY_DATA = "data";
    public static final String KEY_EXTRA = "extra";
  } }

java的https的get请求的更多相关文章

  1. java 实现https请求

    java 实现https请求 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问.但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从 ...

  2. Java发送HTTPS请求

    前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊, ...

  3. Java 发送 Https 请求工具类 (兼容http)

    依赖 jsoup-1.11.3.jar <dependency> <groupId>org.jsoup</groupId> <artifactId>js ...

  4. java 模拟浏览器发送post请求

    java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...

  5. HTTPS那些事 用java实现HTTPS工作原理

    HTTPS那些事 用java实现HTTPS工作原理 博客分类: java历险   今天被问到关于https原理的问题,结果由于知识掌握不牢靠,停留于表面,很多细节都无法回答清楚,于是决定把https的 ...

  6. Java访问HTTPS时证书验证问题

    为了尽可能避免安全问题,公司的很多系统服务都逐步https化,虽然开始过程会遇到各种问题,但趋势不改.最完美的https应用是能实现双向认证,客户端用私钥签名用服务端公钥加密,服务端用私钥签名客户端都 ...

  7. java实现https免证书认证

    java实现https免证书认证   解决方法: 1.下载两个包,httpclient-4.2.jar和httpcore-4.2.jar,复制以下代码就可使用. 2.调用类代码: String htt ...

  8. 【SpringBoot】 Java中如何封装Http请求,以及JSON多层嵌套解析

    前言 本文中的内容其实严格来说不算springboot里面的特性,属于JAVA基础,只是我在项目中遇到了,特归纳总结一下. HTTP请求封装 目前JAVA对于HTTP封装主要有三种方式: 1. JAV ...

  9. 如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查

    我们使用Java的RestTemplate或者Apache的HTTPClient编程的时候,经常遇到需要跟踪Java 代码发送的HTTP请求明细的情况.和javascript代码在浏览器里发送请求可以 ...

随机推荐

  1. C#颠倒字符串

    本函数实现了反转字符串的功能,例如字符串"张赐荣",反转后得到"荣赐张". public static string ReverseText(this stri ...

  2. pypi服务器搭建

    pypi服务器搭建 [root@localhost ~]# hostnamectl set-hostname --static pypi-server [root@pypi-server ~]# yu ...

  3. [Unity] 编辑器运行中动态编译执行C#代码

    (一)问题 之前写Lua时,修改完代码 reload 就可以热重载代码,调试起来十分方便(重构则十分痛苦). 现在使用 C# 做开发,目前还没找到比较方便地进行热重载的方式.只能退而求其次,在调试上找 ...

  4. SaccadeNet:使用角点特征进行two-stage预测框精调 | CVPR 2020

    SaccadeNet基于中心点特征进行初步的目标定位,然后利用初步预测框的角点特征以及中心点特征进行预测框的精调,整体思想类似于two-stage目标检测算法,将第二阶段的预测框精调用的区域特征转化为 ...

  5. C#编程学习(一)

    1.1 开始在Visual Studio 2013环境中编程 控制台应用程序是在命令提示符窗口而非图形用户界面(GUI)中运行的应用程序. 集成开发环境(Integrated Development  ...

  6. [HITCON 2017]SSRFme

    explode()   字符串转数组,用 ,号分隔数组 @mkdir()    创建目录 @chdir() 改变目录 这两的效果一样,如果在/home/php 目录下,执行mkdir('var') 和 ...

  7. AcWing 215. 破译密码

    传送门 思路:gcd(a,b)=k<=>gcd(a/k,b/k)=1,令x=a/k,y=b/k,则问题变为问x<=a/d,y<=b/d有多少(x,y)满足gcd(x,y)=1. ...

  8. ROS入门介绍

    1.ROS版本介绍 ROS版本:(已经推出数十个版本) 2013 ------> Hydro 2014 ------> Indigo (对应Ubuntu14.04) (现在已经基本废弃) ...

  9. 洛谷训练P1008(循环+暴力)

    1 #include<stdio.h> 2 #include<string.h> 3 int a[10]; 4 int main(){ 5 for (int x=123;x&l ...

  10. mysql事务、隔离级别

    一.事务简介 事务是一组操作的集合,它是一一个不可分割的工作单位,事务会把所有的操作作为- -个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功,要么同时失败. 二.有关事务操作 mysql中 ...