C# 后台POST提交方式

1、第一种方式:用最新框架,但是针对IIS服务器的操作系统有关系,非R2的收不到数据;

using (var reqConts = new MultipartFormDataContent())
{
    for (int i = 0; i < colletion.Count; i++)
    {
        HttpPostedFileBase file = colletion[i];
        string extName = Path.GetExtension(file.FileName);
        string fileName = Guid.NewGuid().ToString() + extName;
        var stream = new StreamReader(file.InputStream).BaseStream;
        var streamCont = new StreamContent(stream);
        streamCont.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");//image/*
        streamCont.Headers.ContentLength = stream.Length;
        reqConts.Add(streamCont, "image_binary" + i, fileName);
    }
    var respone = client.PostAsync(uploadUrl, reqConts).Result.Content.ReadAsStringAsync();
    var res = respone.Result;
    Basics.LogFun.Instance().WriteLog(LogFunType.SystemErrLog, "接收发送请求结果:" + res);
    JsonSerializer serializer = new JsonSerializer();
    using (var jsonReader = new JsonTextReader(new StringReader(respone.Result)))
    {
        Result crrresult = serializer.Deserialize<Result>(jsonReader);
        if (crrresult.Success) {            
            return Content(ReturnMessage(true, "处理成功"));
        }
    }
}

2、第二种方式,自己组装,参考代码如下

private string postFile(HttpFileCollectionBase httpFileCollection, string postUrl)
        {

            string encrypt = "f933797503d6e2c36762428a280e0559";
            string boundary = "124559390548109425480397";
            #region 定义请求体中的内容 并转成二进制
            string Enter = "\r\n";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
            request.Method = "POST";
            request.ContentType = "multipart/form-data;boundary=--------------------------" + boundary;
            request.Accept = "*/*";

            Stream myRequestStream = request.GetRequestStream();//定义请求流

            for (int i = 0; i < httpFileCollection.Count; i++)
            {
                HttpPostedFileBase file = httpFileCollection[i];
                string extName = Path.GetExtension(file.FileName);
                string fileName = Guid.NewGuid().ToString() + extName;

                Stream fileStream = file.InputStream;
                byte[] fileContentByte = new byte[fileStream.Length]; // 文件内容二进制
                fileStream.Read(fileContentByte, 0, fileContentByte.Length);
                fileStream.Close();

                string fileContentStr = Enter + "----------------------------" + boundary + Enter
                   + "Content-Type:image/*" + Enter
                   + "Content-Disposition: form-data; name=\"image_binary" + i.ToString() + "\"; filename=\"" + fileName + "\"" + Enter + Enter;

                var fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);//fileContent一些名称等信息的二进制(不包含文件本身)

                myRequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
                myRequestStream.Write(fileContentByte, 0, fileContentByte.Length);
            }
            string encryptStr = Enter + "----------------------------" + boundary + Enter
                    + "Content-Disposition: form-data; name=\"encrypt\"" + Enter + Enter
                    + encrypt + Enter + "--" + boundary + "--";

            var encryptStrByte = Encoding.UTF8.GetBytes(encryptStr);//encrypt所有字符串二进制

            myRequestStream.Write(encryptStrByte, 0, encryptStrByte.Length);

            #endregion

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();//发送

            Stream myResponseStream = response.GetResponseStream();//获取返回值
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

 

posted @ 2019-06-25 15:10  jiajinhao  阅读(3340)  评论(0编辑  收藏  举报