C# webRequest 后端上传文件

发布时间 2023-09-20 13:49:51作者: 谢大山啊

///


/// Http上传文件类 - HttpWebRequest封装
///

public static string PostByForm(UploadParameterType parameter, string token)
{
try
{
using (MemoryStream memoryStream = new MemoryStream())
{
// 1.分界线
string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")), // 分界线可以自定义参数
beginBoundary = string.Format("--{0}\r\n", boundary),
endBoundary = string.Format("\r\n--{0}--\r\n", boundary);
byte[] beginBoundaryBytes = parameter.Encoding.GetBytes(beginBoundary),
endBoundaryBytes = parameter.Encoding.GetBytes(endBoundary);
// 2.组装开始分界线数据体 到内存流中
memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
// 3.组装 上传文件附加携带的参数 到内存流中
if (parameter.PostParameters != null && parameter.PostParameters.Count > 0)
{
foreach (KeyValuePair<string, string> keyValuePair in parameter.PostParameters)
{
string parameterHeaderTemplate = string.Format("Content-Disposition: form-data; name="{0}"\r\n\r\n{1}\r\n{2}", keyValuePair.Key, keyValuePair.Value, beginBoundary);
byte[] parameterHeaderBytes = parameter.Encoding.GetBytes(parameterHeaderTemplate);

                        memoryStream.Write(parameterHeaderBytes, 0, parameterHeaderBytes.Length);
                    }
                }
                // 4.组装文件头数据体 到内存流中

                string fileHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n", parameter.FileNameKey, parameter.FileNameValue);
                byte[] fileHeaderBytes = parameter.Encoding.GetBytes(fileHeaderTemplate);
                memoryStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
                // 5.组装文件流 到内存流中
                byte[] buffer = new byte[1024 * 1024 * 1];
                int size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
                while (size > 0)
                {
                    memoryStream.Write(buffer, 0, size);
                    size = parameter.UploadStream.Read(buffer, 0, buffer.Length);
                }
                // 6.组装结束分界线数据体 到内存流中
                memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
                // 7.获取二进制数据
                byte[] postBytes = memoryStream.ToArray();
                // 8.HttpWebRequest 组装
                System.GC.Collect();
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute));
                webRequest.Method = "POST";
                webRequest.Timeout = 10000;
                webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
                webRequest.ContentLength = postBytes.Length;
                webRequest.KeepAlive = false;
                webRequest.ProtocolVersion = HttpVersion.Version10;
                System.Net.ServicePointManager.DefaultConnectionLimit = 200;
                webRequest.Headers.Add("Authorization", "Bearer " + token);
                if (Regex.IsMatch(parameter.Url, "^https://"))
                {
                    ServicePointManager.Expect100Continue = true;
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)48
                                            | (SecurityProtocolType)192
                                            | (SecurityProtocolType)768
                                            | (SecurityProtocolType)3072;
                    ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
                }
                // 9.写入上传请求数据
                using (Stream requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(postBytes, 0, postBytes.Length);
                    requestStream.Close();
                }
                // 10.获取响应
                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    Stream repStream = webResponse.GetResponseStream();
                    MemoryStream ms = new MemoryStream();
                    repStream.CopyTo(ms);
                    byte[] bt = ms.ToArray();
                    string body = System.Text.Encoding.UTF8.GetString(bt);
                    ms.Close();
                    repStream.Close();
                    webRequest.Abort();
                    webResponse.Close();
                    return body;

                }
            }

        }
        catch (Exception ee)
        {
            
            throw;
        }
      
    }