首页 > 系统相关 >c#-http-2-linux

c#-http-2-linux

时间:2022-10-21 11:48:08浏览次数:46  
标签:files http string stream c# form linux byte data

 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

相关文章

  • MMDetection 安装及使用
    #=============================================#Author:Mikigo#Time:2021/9/1#=============================================一、虚拟环境安装cd~wg......
  • C++调用C#DLL并调试
    使用C++来调用C#DLL并且调试程序环境:使用VSstudio2019C#项目的设置1、C#->属性->应用程序->目标框架->.NETFramework42、C#->属性->调试->调试程......
  • 全网最牛k8s(Kubernetes)+docker+DevOps+linux+微服务容器
     云原生Java架构实战K8s+Docker+KubeSphere+DevOps(中)KubeSphere平台安装简介Kubernetes上安装KubeSphere安装步骤选择4核8G(master)、8核16G(node1)、8核16G(nod......
  • Mac下安装SQLmap的安装
    1、cd/usr/bin/2、sudogitclone ​​https://github.com/sqlmapproject/sqlmap.git​​ sqlmap-dev3、重新打开terminal输入此信息后,回车即可。aliassqlmap=‘​......
  • sysbench--OLTP数据库性能测试与服务器基准功能测试工具
     sysbench是一个基于LuaJIT的可编写脚本的多线程基准测试工具。它最常用于数据库基准测试,但也可用于创建不涉及数据库服务器的任意复杂的工作负载。 sysbench的作用:sys......
  • Centos7 安装docker
    1、安装命令如下:$curl-fsSLhttps://get.docker.com|bash-sdockermirrorAliyun 2、设置仓库$sudoyuminstall-yyum-utils\device-mapper-persistent-data\lvm2......
  • FreeSwitch报错AUDIO RTP REPORTS ERROR: [Bind Error!
    之前弄了,注册用户在公网映射通过,但电话网关一般在同一内网,没多注意。今日有个项目,特殊云,电话用户和网关均在同一侧,不在freeswitch服务端端于是报了个错,电话无法外呼出去......
  • Elasticsearch-----介绍与增删改查操作
    一、介绍:1、概念:是功能非常强大的全文搜索引擎,目的是为了能快速的查询数据2、核心概念:接近实时(NRT):Elasticsearch是一个接近实时的搜索平台。从索引一个文档直到这个文档能......
  • imagemagick: 多图创建gif动图(ImageMagick 6.9.10-86)
    一,命令行例子:1,生成gif[lhdop@bloggif1]$convert-delay200a1.webpa2.webpa3.webpa4.webp-loop0a.gif-delay数字单位是10毫秒,图片切换的时间是该数字乘......
  • Codeforces1695 D1.+D2 Tree Queries
    题意给一个n个点的无向图,其中有一个隐藏点X,可以进行一组询问S来确定S是n个节点中的哪个点。S包括k个询问节点。询问返回的值也为k个值,每个值为X点到每个询问节点的最短路......