string infoOne = "<Request>" + "<MessageHeader><Sender>LIS</Sender><Receiver>DCIS</Receiver>" + "<SendTime>"+ timeNow + "</SendTime>" + "<EventType>REPORT_UPLOAD</EventType>" + "<MsgId>"+ Guid.NewGuid().ToString() + "</MsgId></MessageHeader>" + "<MessageBody>" + "<ReportInfo><PatientId>"+ LisTestDto.Pat_Id+ "</PatientId>" + "</MessageBody></Request>"; byte[] reportImg = reportMogo.ReportImg; //重点: 去除bom Encoding end = new UTF8Encoding(false); string ret = HttpUploadFile("http://-----/api/report/upload", files, new Dictionary<string, string>(), end, infoOne, reportImg); public string HttpUploadFile(string url, string[] files, Dictionary<string, string> data, Encoding encoding, string xmlInfoOnProgram, byte[] reportImg) { try { //必须 string boundary = DateTime.Now.Ticks.ToString("X"); //必须的 //form 开始标志 byte[] boundarybytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); //form 结尾标志 byte[] endbytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); //1.HttpWebRequest HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; request.Method = "POST"; request.KeepAlive = true; request.Credentials = CredentialCache.DefaultCredentials; using (Stream stream = request.GetRequestStream()) { //传递参数模板 Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1} //1.1 key/value string formdataTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}"; //传递参数 此场景没用到这里 if (data != null) { foreach (string key in data.Keys) { //传递参数开始标识 stream.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, data[key]); byte[] formitembytes = encoding.GetBytes(formitem); stream.Write(formitembytes, 0, formitembytes.Length); } } //上传文件模板 //1.2 file string headerTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n"; //byte[] buffer = new byte[6 * 1024 * 1024]; for (int i = 0; i < files.Length; i++) { //上传文件标识 stream.Write(boundarybytes, 0, boundarybytes.Length); //files[1] = "C:\\LikeD\\ReportInfo.xml"; //files[0] = "C:\\LikeD\\Report.pdf"; string header = string.Format(headerTemplate, i == 1 ? "ReportInfo" : "Report", Path.GetFileName(files[i])); byte[] headerbytes = encoding.GetBytes(header); stream.Write(headerbytes, 0, headerbytes.Length); //将文件流读入到请求流中 读取本地用 //using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read)) //{ byte[] buffer = new byte[6 * 1024 * 1024]; if (files[i].Contains("ReportInfo")) { byte[] infoOneBytes = encoding.GetBytes(xmlInfoOnProgram); stream.Write(infoOneBytes, 0, infoOneBytes.Length); } else { stream.Write(reportImg, 0, reportImg.Length); } //} } //form 结束标志 //1.3 form end stream.Write(endbytes, 0, endbytes.Length); } //2.WebResponse HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader stream = new StreamReader(response.GetResponseStream())) { string result = stream.ReadToEnd(); return result; } }catch(Exception ex) { Logger.Error("localpdf" + ex.Message + Environment.NewLine + ex.StackTrace); return ""; } }
标签:files,http,string,stream,c#,form,linux,byte,data From: https://www.cnblogs.com/woainixxx/p/16812906.html