예제 #1
0
        /// <summary>
        /// 收包及反馈
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="PlatformVerifySignatureresule"></param>
        public void DealTarBack(string filepath, out bool PlatformVerifySignatureresule)
        {
            PlatformVerifySignatureresule = false;//验签是否通过
            EBD ebdb = null;

            if (File.Exists(filepath))
            {
                try
                {
                    #region 先删除预处理解压缩包中的文件
                    foreach (string xmlfiledel in Directory.GetFileSystemEntries(ServerForm.strBeUnTarFolder))
                    {
                        if (File.Exists(xmlfiledel))
                        {
                            FileInfo fi = new FileInfo(xmlfiledel);
                            if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                            {
                                fi.Attributes = FileAttributes.Normal;
                            }
                            File.Delete(xmlfiledel);//直接删除其中的文件
                        }
                    }
                    #endregion End

                    ServerForm.tar.UnpackTarFiles(filepath, ServerForm.strBeUnTarFolder);                  //把压缩包解压到专门存放接收到的XML文件的文件夹下

                    string[] xmlfilenames      = Directory.GetFiles(ServerForm.strBeUnTarFolder, "*.xml"); //从解压XML文件夹下获取解压的XML文件名
                    string   sTmpFile          = string.Empty;
                    string   sAnalysisFileName = "";
                    string   sSignFileName     = "";


                    //签名模块  20180820
                    //if (mainForm.m_UsbPwsSupport == "1")
                    //{
                    //    if (xmlfilenames.Length < 2)//没有签名文件
                    //        PlatformVerifySignatureresule = false;
                    //}

                    for (int i = 0; i < xmlfilenames.Length; i++)
                    {
                        sTmpFile = Path.GetFileName(xmlfilenames[i]);
                        if (sTmpFile.ToUpper().IndexOf("EBDB") > -1 && sTmpFile.ToUpper().IndexOf("EBDS_EBDB") < 0)
                        {
                            sAnalysisFileName = xmlfilenames[i];
                        }
                        //else if (sTmpFile.ToUpper().IndexOf("EBDS_EBDB") > -1)//签名文件
                        //{
                        //    sSignFileName = xmlfilenames[i];//签名文件
                        //}
                    }
                    if (!string.IsNullOrWhiteSpace(sAnalysisFileName))
                    {
                        using (FileStream fsr = new FileStream(sAnalysisFileName, FileMode.Open))
                        {
                            StreamReader sr      = new StreamReader(fsr, Encoding.UTF8);
                            string       xmlInfo = sr.ReadToEnd();
                            xmlInfo = xmlInfo.Replace("xmlns:xs", "xmlns");
                            sr.Close();
                            xmlInfo = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlInfo);
                            xmlInfo = XmlSerialize.GetLowOrderASCIICharacters(xmlInfo);
                            ebdb    = XmlSerialize.DeserializeXML <EBD>(xmlInfo);
                        }
                    }
                    string myEBDType = string.Empty;
                    if (ebdb != null)
                    {
                        myEBDType = ebdb.EBDType;
                    }
                    //  if (!string.IsNullOrWhiteSpace(sSignFileName) && myEBDType != "ConnectionCheck")
                    if (myEBDType != "ConnectionCheck")
                    {
                        //读取xml中的文件,转换为byte字节
                        byte[] xmlArray = File.ReadAllBytes(sAnalysisFileName);
                        PlatformVerifySignatureresule = true; //验签结果
                        #region 签名处理  暂时先注释   20180820
                        //Console.WriteLine("开始验证签名文件!");
                        //using (FileStream SignFs = new FileStream(sSignFileName, FileMode.Open))
                        //{
                        //    StreamReader signsr = new StreamReader(SignFs, Encoding.UTF8);
                        //    string xmlsign = signsr.ReadToEnd();
                        //    signsr.Close();
                        //    responseXML signrp = new responseXML();//签名回复
                        //    XmlDocument xmlSignDoc = new XmlDocument();
                        //    try
                        //    {
                        //        int nDeviceHandle = (int)ServerForm.mainFrm.phDeviceHandle;
                        //        xmlsign = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlsign);
                        //        xmlsign = XmlSerialize.GetLowOrderASCIICharacters(xmlsign);
                        //        Signature sign = XmlSerialize.DeserializeXML<Signature>(xmlsign);
                        //        xmlsign = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlsign);
                        //        xmlsign = XmlSerialize.GetLowOrderASCIICharacters(xmlsign);
                        //        string PucStr = sign.SignatureValue;
                        //        byte[] pucsingVi = Encoding.UTF8.GetBytes(sign.SignatureValue);

                        //        //0是签名通过
                        //        var result = ServerForm.mainFrm.usb.PlatformVerifySignature(nDeviceHandle, 1, xmlArray, xmlArray.Length, pucsingVi);
                        //        PlatformVerifySignatureresule = result == 0;
                        //        Log.Instance.LogWrite(PlatformVerifySignatureresule ? "签名验证成功" : "签名验证失败-" + result);
                        //    }
                        //    catch (Exception ex)
                        //    {
                        //        Log.Instance.LogWrite("签名文件错误:" + ex.Message);
                        //    }
                        //}
                        //Console.WriteLine("结束验证签名文件!");
                        #endregion End

                        #region 通用反馈
                        CurrencyTarBack(ebdb);
                        #endregion
                    }

                    ServerForm.DeleteFolder(ServerForm.strBeSendFileMakeFolder);//删除原有XML发送文件的文件夹下的XML
                }
                catch (Exception ep)
                {
                    Log.Instance.LogWrite("处理http异常" + Environment.NewLine + ep.Message);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="PlatformVerifySignatureresule"></param>
        public void DealTarBack(string filepath, out bool PlatformVerifySignatureresule)
        {
            PlatformVerifySignatureresule = false;//验签是否通过
            EBD ebdb = null;

            if (File.Exists(filepath))
            {
                try
                {
                    #region 先删除预处理解压缩包中的文件
                    foreach (string xmlfiledel in Directory.GetFileSystemEntries(ServerForm.strBeUnTarFolder))
                    {
                        if (File.Exists(xmlfiledel))
                        {
                            FileInfo fi = new FileInfo(xmlfiledel);
                            if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                            {
                                fi.Attributes = FileAttributes.Normal;
                            }
                            File.Delete(xmlfiledel);//直接删除其中的文件
                        }
                    }
                    #endregion End

                    ServerForm.tar.UnpackTarFiles(filepath, ServerForm.strBeUnTarFolder);                  //把压缩包解压到专门存放接收到的XML文件的文件夹下

                    string[] xmlfilenames      = Directory.GetFiles(ServerForm.strBeUnTarFolder, "*.xml"); //从解压XML文件夹下获取解压的XML文件名
                    string   sTmpFile          = string.Empty;
                    string   sAnalysisFileName = "";
                    string   sSignFileName     = "";


                    #region  测试注释  20180812
                    //if (mainForm.m_UsbPwsSupport == "1")
                    //{
                    //    if (xmlfilenames.Length < 2)//没有签名文件
                    //        PlatformVerifySignatureresule = false;
                    //}

                    PlatformVerifySignatureresule = true;
                    #endregion

                    for (int i = 0; i < xmlfilenames.Length; i++)
                    {
                        sTmpFile = Path.GetFileName(xmlfilenames[i]);
                        if (sTmpFile.ToUpper().IndexOf("EBDB") > -1 && sTmpFile.ToUpper().IndexOf("EBDS_EBDB") < 0)
                        {
                            sAnalysisFileName = xmlfilenames[i];
                        }
                        //else if (sTmpFile.ToUpper().IndexOf("EBDS_EBDB") > -1)//签名文件
                        //{
                        //    sSignFileName = xmlfilenames[i];//签名文件
                        //}
                    }
                    if (!string.IsNullOrWhiteSpace(sAnalysisFileName))
                    {
                        using (FileStream fsr = new FileStream(sAnalysisFileName, FileMode.Open))
                        {
                            StreamReader sr      = new StreamReader(fsr, Encoding.UTF8);
                            string       xmlInfo = sr.ReadToEnd();
                            xmlInfo = xmlInfo.Replace("xmlns:xs", "xmlns");
                            sr.Close();
                            xmlInfo = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlInfo);
                            xmlInfo = XmlSerialize.GetLowOrderASCIICharacters(xmlInfo);
                            ebdb    = XmlSerialize.DeserializeXML <EBD>(xmlInfo);
                        }
                    }
                    string myEBDType = string.Empty;
                    if (ebdb != null)
                    {
                        myEBDType = ebdb.EBDType;
                    }
                    if (!string.IsNullOrWhiteSpace(sSignFileName) && myEBDType != "ConnectionCheck")
                    {
                        //读取xml中的文件,转换为byte字节
                        byte[] xmlArray = File.ReadAllBytes(sAnalysisFileName);

                        //#region 签名处理
                        //Console.WriteLine("开始验证签名文件!");
                        //using (FileStream SignFs = new FileStream(sSignFileName, FileMode.Open))
                        //{
                        //    StreamReader signsr = new StreamReader(SignFs, Encoding.UTF8);
                        //    string xmlsign = signsr.ReadToEnd();
                        //    signsr.Close();
                        //    responseXML signrp = new responseXML();//签名回复
                        //    XmlDocument xmlSignDoc = new XmlDocument();
                        //    try
                        //    {
                        //        int nDeviceHandle = (int)ServerForm.mainFrm.phDeviceHandle;
                        //        xmlsign = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlsign);
                        //        xmlsign = XmlSerialize.GetLowOrderASCIICharacters(xmlsign);
                        //        Signature sign = XmlSerialize.DeserializeXML<Signature>(xmlsign);
                        //        xmlsign = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlsign);
                        //        xmlsign = XmlSerialize.GetLowOrderASCIICharacters(xmlsign);
                        //        string PucStr = sign.SignatureValue;
                        //        byte[] pucsingVi = Encoding.UTF8.GetBytes(sign.SignatureValue);

                        //        //0是签名通过
                        //        var result = ServerForm.mainFrm.usb.PlatformVerifySignature(nDeviceHandle, 1, xmlArray, xmlArray.Length, pucsingVi);
                        //        PlatformVerifySignatureresule = result == 0;
                        //        Log.Instance.LogWrite(PlatformVerifySignatureresule ? "签名验证成功" : "签名验证失败-" + result);
                        //    }
                        //    catch (Exception ex)
                        //    {
                        //        Log.Instance.LogWrite("签名文件错误:" + ex.Message);
                        //    }
                        //}
                        //Console.WriteLine("结束验证签名文件!");
                        //#endregion End


                        #region 通用反馈
                        CurrencyTarBack(ebdb);
                        #endregion
                    }
                    ServerForm.DeleteFolder(ServerForm.strBeSendFileMakeFolder);//删除原有XML发送文件的文件夹下的XML
                    if (!PlatformVerifySignatureresule && myEBDType != "ConnectionCheck")
                    {
                        #region //这里加个失败的反馈
                        try
                        {
                            XmlDocument xmlDoc = new XmlDocument();
                            responseXML rp     = new responseXML();
                            rp.SourceAreaCode = ServerForm.strSourceAreaCode;
                            rp.SourceType     = ServerForm.strSourceType;
                            rp.SourceName     = ServerForm.strSourceName;
                            rp.SourceID       = ServerForm.strSourceID;
                            rp.sHBRONO        = ServerForm.strHBRONO;

                            Random rd = new Random();
                            // string fName = "10" + rp.sHBRONO + "00000000000" + rd.Next(100, 999).ToString();
                            string fName = "10" + rp.sHBRONO + "0000000000000" + rd.Next(100, 999).ToString();
                            xmlDoc = rp.VerifySignatureError(ebdb, "EBDResponse", fName);
                            string xmlSignFileName = "\\EBDB_" + fName + ".xml";

                            CreateXML(xmlDoc, ServerForm.strBeSendFileMakeFolder + xmlSignFileName);

                            //进行签名
                            //    ServerForm.mainFrm.GenerateSignatureFile(ServerForm.strBeSendFileMakeFolder, fName); 测试注释20180812
                            ServerForm.tar.CreatTar(ServerForm.strBeSendFileMakeFolder, ServerForm.sSendTarPath, fName);//使用新TAR
                            string       sSendTarName = ServerForm.sSendTarPath + "\\EBDT_" + fName + ".tar";
                            FileStream   fsSnd        = new FileStream(sSendTarName, FileMode.Open, FileAccess.Read);
                            BinaryReader br           = new BinaryReader(fsSnd); //时间戳
                            int          datalen      = (int)fsSnd.Length + 2;
                            int          bufferLength = 4096;
                            long         offset       = 0; //开始上传时间
                            writeHeader(datalen.ToString(), "EBDT_" + fName + ".tar");

                            byte[] buffer = new byte[4096]; //已上传的字节数
                            int    size   = br.Read(buffer, 0, bufferLength);
                            while (size > 0)
                            {
                                outputStream.Write(buffer, 0, size);
                                offset += size;
                                size    = br.Read(buffer, 0, bufferLength);
                            }
                            outputStream.Write(Encoding.UTF8.GetBytes(sEndLine), 0, 2);
                            outputStream.Flush();//提交写入的数据
                            fsSnd.Close();
                        }
                        catch (Exception esb)
                        {
                            Console.WriteLine("401:" + esb.Message);
                        }
                        #endregion
                        return;
                    }
                }
                catch (Exception ep)
                {
                    Log.Instance.LogWrite("处理http异常" + Environment.NewLine + ep.Message);
                }
            }
        }