zoukankan      html  css  js  c++  java
  • 多文件多参数中转客户端(转卢震的)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Collections.Specialized;

    namespace Controller.Interface.Area
    {

        public class Client
        {

            public static string UploadFileEx( string url,
                List<Stream> fileStream, NameValueCollection querystring
                )
            {
                string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
                HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
                webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
                webrequest.Method = "POST";
                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(boundary);
                sb.Append("\r\n");
                sb.Append("Content-Disposition: form-data; name=\"");
                sb.Append("piaoClient");
                sb.Append("\"; filename=\"");
                sb.Append("piaoClient");
                sb.Append("\"");
                sb.Append("\r\n");
                sb.Append("Content-Type: ");
                sb.Append(contenttype);
                sb.Append("\r\n");
                sb.Append("\r\n");
                StringBuilder sb1 = new StringBuilder();
                foreach (var key in querystring.Keys)
                {
                    sb1.Append("--");
                    sb1.Append(boundary);
                    sb1.Append("\r\n");
                    sb1.Append("Content-Disposition: form-data; name=\""+key.ToString()+"\"");
                    sb1.Append("\r\n");
                    sb1.Append("\r\n");
                    sb1.Append(querystring[key.ToString()].ToString());
                    sb1.Append("\r\n");
                }
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sb.ToString());
                byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
                byte[] postDate = Encoding.UTF8.GetBytes(sb1.ToString());
                long length = postHeaderBytes.Length * fileStream.Count + fileStream.Sum(s1 => s1.Length) + (boundaryBytes.Length * fileStream.Count) + postDate.Length;
                webrequest.ContentLength = length;
                Stream requestStream = webrequest.GetRequestStream();
                requestStream.Write(postDate, 0, postDate.Length);
                foreach (var filestream1 in fileStream)
                {
                    requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                    byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)filestream1.Length))];
                    int bytesRead = 0;
                    while ((bytesRead = filestream1.Read(buffer, 0, buffer.Length)) != 0)
                        requestStream.Write(buffer, 0, bytesRead);
                    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                }
                webrequest.Timeout = 1000000;
                WebResponse responce = webrequest.GetResponse();
                Stream s = responce.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                string str = sr.ReadToEnd();
                foreach (var filestream1 in fileStream)
                {
                    filestream1.Close();
                }
                requestStream.Close();
                sr.Close();
                s.Close();
                responce.Close();
                return str;
            }
        }
    }

  • 相关阅读:
    React 项目 ant design 的 CheckboxGroup 验证
    React 项目中修改 Ant Design 的默认样式(Input Checkbox 等等
    create-react-app 构建的项目使用释放配置文件 webpack 等等 运行 npm run eject 报错
    使用 nodejs 和 axios 以及 cherrio 爬取天气预报
    ant design Radio.Group defaultValue 默认选中没生效
    macOS 更新 git 命令提示 xcrun,.gitignore 配置不生效问题。
    mac 绑定阿里企业邮箱
    create-react-app 构建的项目使用 mobx (说到底就是为了使用装饰器语法对 babel 做些配置
    React 项目使用 React-router-dom 4.0 以上版本时使用 HashRouter 怎么控制 history
    js 操作css
  • 原文地址:https://www.cnblogs.com/Love/p/1323718.html
Copyright © 2011-2022 走看看