/// <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); } } }
/// <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); } } }
public static model.EBDResponse UploadFilesByPost(string address, string fileNamePath, int state) { try { model.EBDResponse Res = new model.EBDResponse(); int u = ServicePointManager.DefaultConnectionLimit; ServicePointManager.DefaultConnectionLimit = 200; string returnValue = "0"; // 要上传的文件 WebResponse webRespon = null; FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); //时间戳 string sguidSplit = Guid.NewGuid().ToString(); string filename = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1); StringBuilder sb = new StringBuilder(300); string strPostHeader = sb.ToString(); HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(address)); httpReq.ServicePoint.Expect100Continue = false; httpReq.Method = "POST"; //对发送的数据不使用缓存 httpReq.AllowWriteStreamBuffering = false; //设置获得响应的超时时间(300秒) httpReq.Timeout = 80000; httpReq.ContentType = "multipart/form-data; boundary=" + sguidSplit; //"text/xml"; httpReq.Accept = "text/plain, */*"; httpReq.UserAgent = "WinHttpClient"; httpReq.Headers["Accept-Language"] = "zh-cn"; sb.Append("--" + sguidSplit + "\r\n"); sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n"); sb.Append("Content-Type: application/octet-stream;Charset=UTF-8\r\n"); sb.Append("\r\n"); byte[] boundaryBytes = Encoding.ASCII.GetBytes(sb.ToString()); //请求头部信息 byte[] bEndBytes = Encoding.ASCII.GetBytes("\r\n--" + sguidSplit + "--\r\n"); long length = fs.Length + boundaryBytes.Length + bEndBytes.Length; long fileLength = fs.Length; httpReq.ContentLength = length; try { int bufferLength = 4096; //每次上传4k byte[] buffer = new byte[bufferLength]; //已上传的字节数 long offset = 0; //开始上传时间 DateTime startTime = DateTime.Now; int size = r.Read(buffer, 0, bufferLength); Stream postStream = httpReq.GetRequestStream(); //发送请求头部消息 postStream.Write(boundaryBytes, 0, boundaryBytes.Length); while (size > 0) { postStream.Write(buffer, 0, size); offset += size; TimeSpan span = DateTime.Now - startTime; double second = span.TotalSeconds; Application.DoEvents(); size = r.Read(buffer, 0, bufferLength); } //添加尾部的时间戳 postStream.Write(bEndBytes, 0, bEndBytes.Length); postStream.Close(); //获取服务器端的响应 Encoding encoding = Encoding.UTF8; //根据网站的编码自定义 webRespon = httpReq.GetResponse(); //提示操作超时 20180105 Stream responseStream = webRespon.GetResponseStream(); string str = streamReadLine(responseStream); if (str != "") { string xmlInfo = GetResponseXml(str); xmlInfo = XmlSerialize.GetLowOrderASCIICharacters(xmlInfo); model.EBD ebd = XmlSerialize.DeserializeXML <model.EBD>(xmlInfo); if (ebd != null) { Res.ResultCode = ebd.EBDResponse.ResultCode; Res.ResultDesc = ebd.EBDResponse.ResultDesc; } responseStream.Close(); } else { return(null); } } catch (Exception ex) { Console.WriteLine(ex); } finally { fs.Close(); r.Close(); if (httpReq != null) { httpReq.Abort(); } if (webRespon != null) { webRespon.Close(); } GC.Collect(); } return(Res); } catch (Exception ex) { Console.WriteLine(ex); Thread.Sleep(500); } return(null); }