使用Socket获取银联返回的刷卡信息

 

 

public synchronized String sendAskToUnionPay(String encryptText) throws Exception {
  
  Socket socket = null;
  OutputStream output = null; // 输入流
  DataInputStream   input = null;
  InetSocketAddress inetSocketAddress = null;
  String text = null;
  int communicateTime = 0; // 当前已尝试连接的次数
  try {
   Properties props = FTPConfiger.getProperties();//获取配置文件
   String socketIP = props.getProperty("socketIP");// 从配置文件中获取参数IP
   int socketProt = Integer.parseInt(props.getProperty("socketProt"));// 端口
   int socketConnectionCount = Integer.parseInt(props.getProperty("socketConnectionCount"));// 最大连接次数
   int socketConnectionTime = Integer.parseInt(props.getProperty("socketConnectionTime"));// 超时时间
   int socketWateServerTime = Integer.parseInt(props.getProperty("socketWateServerTime"));// 超时时间
   String bankSocketEndTag = props.getProperty("bankSocketEndTag");// 从配置文件中获取参数IP
   inetSocketAddress = new InetSocketAddress(socketIP, socketProt);
   // ------------创建连接------------
   socket = new Socket();
   while (communicateTime < socketConnectionCount) {
    socket.connect(inetSocketAddress, socketConnectionTime * 1000);
    communicateTime++;
    if (socket.isConnected()) {
     System.out.println("连接服务器成功!");
     log.debug("连接服务器成功!");
     break;
    }else{
     log.debug("连接服务器失败!");
     throw new BusinessException("连接服务器失败!");
    }
   }
   if (socket.isConnected()) {
    output = socket.getOutputStream();//基于Soacket返回输出流
    output.write(encryptText.getBytes());
    output.flush();// 发送信息至银联
    input = new DataInputStream(socket.getInputStream());//基于Soacket返回输入流
    int avali = 0;
    for (int i = 0;i < socketWateServerTime; i++){
     avali = input.available();
     if (avali == 0){
      Thread.sleep(1000);
      System.out.println("银行没有返回信息,please wait......");
     }else{
      Util comUtil = Util.getUtil();
//      ---------------------------------获取流的头信息,
      byte aval[] = new byte[6];
      input.read(aval,0,6);
      String strCount = comUtil.decodeStr(aval, "GB2312");
      System.out.println("服务器返回的头信息是 :" + strCount);
      int count = Integer.parseInt(strCount.substring(0,4));//记录数
      String returnTitle = strCount.substring(4,6);
//      ---------------------------------
      if (count > 0 && (returnTitle.equals("00") || returnTitle.equals("04"))){
       byte tmpdata[] = new byte[count * 2 * 1660 ];
       int nowIndex = 0;
       byte bTmp;
//      ------------------------------------循环从流中读取数据,知道读取不到为止
       while ((bTmp = (byte)input.read()) != -1) {
        if (input.available() ==0) {
         for (int j= 0;j<5;j++){
          if (input.available() != 0) break;
          else Thread.sleep(1000);
         }
         log.debug("没有获取到数据,可能是因为网络现在过于拥挤,请稍后再试!!!");
         throw new BusinessException("没有获取到数据,可能是因为网络现在过于拥挤,请稍后再试!!!");
        }
        tmpdata[nowIndex] = bTmp;
        nowIndex ++;
       }
       if (output != null) output.close();
       if (input != null) input.close();
       if (socket != null) socket.close();
//      ------------------------------------
       if (tmpdata.length > 0)
        text = comUtil.decodeStr(tmpdata,"GB2312").trim();
       else
        throw new BusinessException("银联返回的数据长度为null......要怎么办呢?");
//      --------------------------------------
       int endTagNum = bankSocketEndTag.length();
       int textNum =text.length();
       if (text.substring(textNum - endTagNum,textNum).equals(bankSocketEndTag)){
        text =  text.substring(0,textNum - endTagNum);
       }
       text =  strCount + text;
       log.debug("获取到的数据是 >>>>>>>:" + text);
       System.out.println("获取到的数据是 >>>>>>>:" + text);
       break;
      }
     }
    }
   }
   return text;
  } catch (UnknownHostException e) {
   log.error("连接中断,中断的原因是: " + e.getMessage());
   throw new BusinessException("连接中断,中断的原因是: " + e.getMessage());
  } catch (IOException e) {
   log.error("连接中断,中断的原因是: " + e.getMessage());
   throw new  BusinessException("连接中断,中断的原因是: " + e.getMessage());
  } finally {
   if (output != null) output.close();
   if (input != null) input.close();
   if (socket != null) socket.close();
  }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值