基于Https post get方式通讯协议数据访问接口

    /**
    * 基于Https post 方式通讯协议数据访问接口
    * @param ipAddress ip地址
    * @param visitUrl 通讯接口地址
    * @param userName 用户账号
    * @param passWord 用户密码
    * @return
    */
   public static Map<String,Object> doPost(String ipAddress,String visitUrl,String userName,String passWord)
   {
      Map<String,Object> resultMap = new HashMap<String,Object>();

      String resultStr = "";
      int resultCode = 0;
      CredentialsProvider credsProvider = new BasicCredentialsProvider();
      credsProvider.setCredentials(new AuthScope(ipAddress, 5000),new UsernamePasswordCredentials(userName, passWord));

      CloseableHttpClient httpclient = null;
      CloseableHttpResponse response = null;
      try
      {
         SSLContext sslContext = SSLContexts.custom().loadTrustMaterial((chain, authType)->true).build();
         SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory (sslContext,
            new String[] {"SSLv2Hello", "SSLv3", "TLSv1","TLSv1.1", "TLSv1.2" },
            null,NoopHostnameVerifier.INSTANCE);

         httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
            .setSSLSocketFactory(sslConnectionSocketFactory)
            .build();

         HttpPost httppost = new HttpPost(visitUrl);
//       String postParams ="{\"strategy\":\"rf\",\"frequency\":[\"2.4GHz\",\"5.8GHz\"],\"duration\":60}";
//       System.out.println(postParams);
//       HttpEntity httpEntity = new StringEntity(postParams);
//       httppost.setEntity(httpEntity);
         RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(20000)
            .setConnectionRequestTimeout(20000)
            .setSocketTimeout(20000).build();
         httppost.setConfig(requestConfig);
         response = httpclient.execute(httppost);

         resultCode = response.getStatusLine().getStatusCode();
      // resultStr = EntityUtils.toString(response.getEntity());
         resultMap.put("resultCode", resultCode);
         resultMap.put("resultStr", resultStr);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if(response != null)
         {
            try
            {
               response.close();
            }
            catch (IOException e)
            {
               e.printStackTrace();
            }
         }

         if(httpclient != null)
         {
            try
            {
               httpclient.close();
            }
            catch (IOException e)
            {
               e.printStackTrace();
            }
         }
      }


      return resultMap;
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值