C# 接口实例

private void Analysis(string filePath) { //DirectoryInfo directoryInfo = new DirectoryInfo(filePath); //FileInfo[] fileInfo = directoryInfo.GetFiles(); var directoryInfo = new DirectoryInfo(filePath); //文件夹所在目录 var fc = new FileComparer(); FileInfo[] fileInfo = directoryInfo.GetFiles(); Array.Sort(fileInfo, fc); //按文件创建时间排正序 if (fileInfo.Count() == 0) { //MessageBox.Show("解析路径所选择文件不包含任何文件!"); return; } else { foreach (var fileItem in fileInfo) { string fileExt = fileItem.Extension; if (fileExt == ".xml"|| fileExt == ".NEMS") { string xmlPath = fileItem.FullName; string GetFileName = Path.GetFileName(xmlPath); string fullName = Path.GetDirectoryName(xmlPath); string Name = fullName.Substring(fullName.LastIndexOf("\\")); string FillName = xmlPath.Substring(xmlPath.LastIndexOf("\\") + 1, (xmlPath.LastIndexOf(".") - xmlPath.LastIndexOf("\\") - 1)); string partName = FillName.Substring(FillName.IndexOf("_",0)+1); string FileName = partName.Substring(0, partName.LastIndexOf("_")); string fileName = fileItem.Name; //读取xml内容 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); var content = xmlDoc; var xmlStr = content.InnerXml; var base64Str = HttpHelper.EncodeBase64(xmlStr, "utf-8"); GetPostData getPostData = https://www.it610.com/article/new GetPostData(); bool result = true; string ETPSNO1=ETPSNO.Text; var returnstrs = getPostData.HttpPost1(base64Str, FileName, fileExt, out result, ETPSNO1); if (!result) { MessageBox.Show(returnstrs); return; } XmlMsg xmlMsg = (XmlMsg)JsonHelper.jsonDes(returnstrs); //MessageBox.Show("文件路径" + xmlPath + "接口状态:" + xmlMsg.status); if (xmlMsg.status == "0") { string FailBox = fullName.Substring(0, fullName.LastIndexOf("\\")) + "\\FailBox"; if (!Directory.Exists(FailBox)) { Directory.CreateDirectory(FailBox); } //将申报失败的文件移动到FailBox文件夹 string FdelPath = Path.Combine(FailBox, fileName); File.Move(xmlPath, FdelPath); MessageBox.Show("申报解析文件失败"); } if (xmlMsg.status == "1") { //MessageBox.Show("申报解析文件成功"); //string TempDelFile = fullName.Substring(0, fullName.LastIndexOf("\\")); string OverInBox = fullName.Substring(0, fullName.LastIndexOf("\\")) + "\\OverInBox"; if (!Directory.Exists(OverInBox)) { Directory.CreateDirectory(OverInBox); }//将申报成功的文件移动到OverInBox文件夹 string delPath = Path.Combine(OverInBox, fileName); File.Move(xmlPath, delPath); //File.Delete(delPath); //删除 XmlNode xmlNodeEPN = xmlDoc.SelectSingleNode("//DealFlag"); string DealFlag = xmlNodeEPN.InnerXml; if (DealFlag == "-1") { bool result3 = true; var strs = getPostData.HttpPostPath(delPath, fileName,out result3); if (!result3) { MessageBox.Show(strs); return; } XmlMsg xmlMsgs = (XmlMsg)JsonHelper.jsonDes(strs); if (xmlMsgs.status == "0") { MessageBox.Show("错误回执文件信息路径更新失败"); } if (xmlMsgs.status == "1") { MessageBox.Show("已将错误回执文件信息路径更新成功"); } } } } } } }public string HttpPost1(string jsonStr, string FileName, string fileExt, out bool result,string ETPSNO) { result = true; string str = null; string url = IniStaticField.postUrl + "/api/MessageXML/AnalysisMessage"; string postStr= "{ \"jsonStr\" :\"" + jsonStr + "\",\"FileName\" :\"" + FileName + "\",\"fileExt\" :\"" + fileExt + "\",\"ETPSNO\" :\"" + ETPSNO + "\"}"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; UTF8Encoding utf8 = new UTF8Encoding(); byte[] byteArray = utf8.GetBytes(postStr); request.ContentLength = byteArray.Length; try { System.IO.Stream postStream = request.GetRequestStream(); postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); } catch (WebException ex) { var wrongs = ex.Status; result = false; return ex.ToString(); }HttpWebResponse res = null; StreamReader reader = null; try { res = (HttpWebResponse)request.GetResponse(); reader = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("utf-8")); str = reader.ReadToEnd(); return str; } catch (WebException ex) { res = (HttpWebResponse)ex.Response; MessageBox.Show("解析Xml文件发生错误,错误信息为:" + ex.Message); result = false; return ex.ToString(); } finally { if (reader != null) { reader.Close(); reader.Dispose(); } if (res != null) { res.Close(); }} }

    推荐阅读