⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mm7sender.java

📁 中国移动彩信中心MM7接口统一API mm7api
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                                pool.
                                getNonceCount(), 8) + 1));
                            connWrap.setDigestInfor(setDigestAuth(authInfor));
                            continue;
                          }
                        }
                      }
                      //开始接收http体
                      if (totalresline == httpseper + contlen3 + 4) {
                      	/*
                      	if(this.TimeOutFlag==true){
                      		SevereBuffer.append("baos.tostring()=="+baos.toString());
                      	}*/
                        if (pool.getKeepAlive().equals("off")) {
                          sender.close();
                          receiver.close();
                          client.close();
                        }
                        else {
                          connWrap.setUserfulFlag(true);
                          connWrap.setFree(true);
                        }
                        flag = true;
                        bReceive = true;
                        break;
                      }
                    }
                  }
                  else if (baos.toString().indexOf("Basic") != -1) {
                    if (ResendCount < mm7Config.getReSendCount()) {
                      ResendCount = ResendCount + 1;
                      receiveMessge();
                    }
                    else {
                      bReceive = false;
                      break;
                    }
                  }
                }
                else {
                  bReceive = false;
                  break;
                }
              }
              else {
                int index1 = baos.toString().toLowerCase().indexOf(MMConstants.
                    CONTENT_LENGTH);
                int index2 = baos.toString().indexOf("\r", index1);
                int contlength = 0;
                if (index1 == -1 || index2 == -1) {
                  int encodingindex = baos.toString().toLowerCase().indexOf(
                      "transfer-encoding:");
                  if (encodingindex >= 0) {
                    int encodingend = baos.toString().indexOf("\r\n",
                        encodingindex);
                    if (encodingend >= 0) {
                      String strenc = baos.toString().substring(encodingindex +
                          "transfer-encoding:".length(), encodingend).trim();
                      if (strenc.equalsIgnoreCase("chunked")) {
                        ////在这里可以增加chunked的处理

                        int endencindex2 = baos.toString().indexOf("\r\n",
                            encodingend + 1);
                        if (endencindex2 >= 0) {
                          int xmlbeg = baos.toString().indexOf(MMConstants.
                              BEGINXMLFLAG, endencindex2 + 1);
                          if (xmlbeg > 0) {
                            String strTempContLen = baos.toString().substring(
                                endencindex2, xmlbeg).trim();
                            contlength = Integer.parseInt(strTempContLen, 16);
                          }
                        }
                      }
                      else {
                        bReceive = false;
                        break;
                      }
                    }
                    else {
                      bReceive = false;
                      break;
                    }
                  }
                  else {
                    continue;
                  }

                  if (totalline >= httpsepindex + contlength + 8) {
                  	SevereBuffer.append("receive end");
                  	SevereBuffer.append("baos.toString()=="+baos.toString());
                    if (pool.getKeepAlive().equals("off")) {
                      sender.close();
                      receiver.close();
                      client.close();
                    }
                    else {
                      connWrap.setUserfulFlag(true);
                      connWrap.setFree(true);
                    }
                    bReceive = true;
                    break;
                  }

                }
                else {
                  contlength = Integer.parseInt(baos.toString().substring(
                      (index1 + MMConstants.CONTENT_LENGTH.length() + 1),
                      index2).
                                                trim());
                  //开始接收http体
                  if (totalline == httpsepindex + contlength + 4) {
                    //System.out.println("正常接收结束");
                    //add by hudm to test 104 problem 2004-06-09
                    if(this.TimeOutFlag==true)
                      SevereBuffer.append("baos.tostring()=="+baos.toString());
                    //end add by hudm
                    if (pool.getKeepAlive().equals("off")) {
                      sender.close();
                      receiver.close();
                      client.close();
                    }
                    else {
                      connWrap.setUserfulFlag(true);
                      connWrap.setFree(true);
                    }
                    bReceive = true;
                    break;
                  }
                }
              }
            }
            else{
              bReceive = false;
              break;
            }

          }
        }
        /*else
        {
          //System.out.println("第1083行。");
          this.TimeOutFlag = true;
          sendBaos = getSendMessage(this.TimeOutbCount);
          if (sendBaos != null) {
            //System.out.println("第1087行。");
            res = SendandReceiveMessage(sendBaos);
            if(res != null)
            {
              this.TimeOutFlag = false;
              return true;
            }
         }
         else
           return false;
        }*/
      }
      return bReceive;
    }
   catch (SocketTimeoutException ste) {
     //System.out.println("第1100行。");
     this.TimeOutFlag = true;
      ReadTimeOutCount++;
    if(ReadTimeOutCount< mm7Config.getReSendCount()) {
       sendBaos = getSendMessage(this.TimeOutbCount);
       if(sendBaos != null)
       {
         res = SendandReceiveMessage(sendBaos);
         if(res != null){
           this.TimeOutFlag = false;
           return true;
         }
         /*if (res.getStatusCode() == MMConstants.RequestStatus.SUCCESS) {
           this.TimeOutFlag = false;
           return true;
         }*/
       }
       else
         return false;
     }
     this.TimeOutFlag = false;
     res.setStatusCode( -101);
     res.setStatusText("超时发送失败!");
     SevereBuffer.append("[Comments={-101;"+res.getStatusText()+"}]");
     ReadTimeOutCount = 0;
     return false;
   }
  }


  /**输入string,输出经过MD5编码的String*/
  public String calcMD5(String str) {
    try {
      MessageDigest alga = MessageDigest.getInstance("MD5");
      alga.update(str.getBytes());
      byte[] digesta = alga.digest();
      return byte2hex(digesta);
    }
    catch (NoSuchAlgorithmException ex) {
      System.out.println("出错了!!没有这种算法!");
    }
    return "NULL";
  }

  //byte[]数组转成字符串
  public String byte2hex(byte[] b) {
    String hs = "";
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
      stmp = (Integer.toHexString(b[n] & 0XFF));
      if (stmp.length() == 1)
        hs = hs + "0" + stmp;
      else
        hs = hs + stmp;
      if (n < b.length - 1)
        hs = hs + "";
    }
    return hs;
  }

  private String setDigestAuth(String authinfor)
  {
    String auth = authinfor +"\r\n";
    int equal = -1;
    int comma = -1;
    StringBuffer authBuffer = new StringBuffer();
    authBuffer.append("Authorization: Digest ");
    String qopValue;
    String MD5A2;
    String algValue;
    String cnonceValue= "";
    String ncValue;
    String responseValue;
    String uri = mm7Config.getMMSCURL();
    if(uri == null)
        uri = "";
    String username = mm7Config.getUserName();
    authBuffer.append("uri=\""+uri+"\",");
    authBuffer.append("username=\""+username+"\",");

    String passwd = mm7Config.getPassword();
    // 获得realm的值
    int realmindex = auth.indexOf(MMConstants.REALM);
    equal = auth.indexOf("=", realmindex + 1);
    comma = auth.indexOf("\"", equal+2);
    String realmValue = auth.substring(equal+1,comma);
    if(realmValue.startsWith("\""))
      realmValue = realmValue.substring(1,realmValue.length());
    authBuffer.append("realm=\""+realmValue+"\",");
    String MD5A1;
    //取得nonce的值
    int nonceindex = auth.indexOf(MMConstants.NONCE);
    equal = auth.indexOf("=",nonceindex+1);
    comma = auth.indexOf("\"",equal+2);
    String nonceValue = auth.substring(equal+1,comma);
    if(nonceValue.startsWith("\""))
      nonceValue = nonceValue.substring(1,nonceValue.length());
    authBuffer.append("nonce=\""+nonceValue+"\",");
    //判断有没有opaque,若有,则原封不动地返回给服务器
    int opaqueindex = auth.indexOf(MMConstants.OPAQUE);
    if(opaqueindex >0)
    {
      equal = auth.indexOf("=",opaqueindex+1);
      comma = auth.indexOf("\"",equal+2);
      authBuffer.append("opaque="+auth.substring(equal+1,comma+1));
    }
    //取得algorithm的值
    int algindex = auth.indexOf(MMConstants.ALGORITHM);
    if(algindex>0)
    {
      equal = auth.indexOf("=", algindex);
      comma = auth.indexOf(",", equal+2);
      if(comma >=0)
      {
        algValue = auth.substring(equal + 1, comma);
        if (algValue.startsWith("\""))
          algValue = algValue.substring(1, algValue.length() - 1);
      }
      else
      {
        comma = auth.indexOf("/r",equal);
        algValue = auth.substring(equal + 1, comma);
        if (algValue.startsWith("\""))
          algValue = algValue.substring(1, algValue.length());
      }
    }
    else
    {
      algValue = "MD5";
    }
    //取得qop的值
    int qopindex = auth.indexOf(MMConstants.QOP);
    if(algValue.equals("MD5") ||algValue.equals("MD5-sess"))
    {
      MD5A1 = calcMD5(username + ":" + realmValue + ":" + passwd);
      //服务器存在qop这个字段
      if (qopindex > 0) {
        equal = auth.indexOf("=", qopindex);
        comma = auth.indexOf("\"", equal+2);
        qopValue = auth.substring(equal + 1, comma);
        if (qopValue.startsWith("\""))
          qopValue = qopValue.substring(1, qopValue.length());
          /**表明服务器给出了两种qop,为auth和auth-int,
           * 这是应该是客户端自己选择采用哪种方式进行鉴权
           * */
        if (qopValue.indexOf(",") > 0) {
          if (mm7Config.getDigest().equals("auth-int")) {
            MD5A2 = calcMD5("POST" + ":" + uri + ":" +
                            calcMD5(entityBody.toString()));
          }
          else {
            MD5A2 = calcMD5("POST" + ":" + uri);
          }
          ncValue = String.valueOf(pool.getNonceCount());
          cnonceValue = getBASE64(ncValue);
          responseValue = calcMD5(MD5A1 + ":" + nonceValue + ":" + ncValue +
                                  ":" + cnonceValue + ":" + qopValue + ":" + MD5A2);
          authBuffer.append("qop=\""+mm7Config.getDigest()+"\",");
          authBuffer.append("nc="+ncValue+",");
          authBuffer.append("cnonce=\""+cnonceValue+"\",");
          authBuffer.append("response=\""+responseValue+"\",");
        }
        //服务器端只有一种qop方式。
        else
        {
          if (qopValue.equals("auth-int")) {
            MD5A2 = calcMD5("POST" + ":" + uri + ":" +
                            calcMD5(entityBody.toString()));
          }
          else
            MD5A2 = calcMD5("POST" + ":" + uri);
          ncValue = String.valueOf(pool.getNonceCount());
          cnonceValue = getBASE64(ncValue);
          responseValue = calcMD5(MD5A1 + ":" + nonceValue + ":" + ncValue +
                                  ":" + cnonceValue + ":" + qopValue + ":" + MD5A2);
          authBuffer.append("qop=\""+qopValue+"\",");
          authBuffer.append("nc="+ncValue+",");
          authBuffer.append("cnonce=\""+cnonceValue+"\",");
          authBuffer.append("response=\""+responseValue+"\",");
        }
      }
      //服务器端不存在对qop的方式的选择
      else {
        MD5A2 = calcMD5("POST" + ":" + uri);
        responseValue = calcMD5(MD5A1 + ":" + nonceValue + ":" + MD5A2);
        authBuffer.append("response=\""+responseValue+"\",");
      }
    }
    //去掉最后一个逗号
    int lastcommaindex = authBuffer.lastIndexOf(",");
    authBuffer.delete(lastcommaindex,lastcommaindex+1);
    authBuffer.append("\r\n");
    return authBuffer.toString();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -