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;
            }
        }
    }

  • 相关阅读:
    Vue-Router路由 Vue-CLI脚手架和模块化开发 之 路由常用配置与路由嵌套
    (最大上升子序列) Super Jumping! Jumping! Jumping! -- hdu -- 1087
    最大连续子序列 -- hdu -- 1231
    (KMP灵活运用 利用Next数组 )Theme Section -- hdu -- 4763
    (KMP 水)Wow! Such Doge! -- hdu -- 4847
    (回文串 Manacher )Girls' research -- hdu -- 3294
    (回文串 Manacher)吉哥系列故事——完美队形II -- hdu -- 4513
    (回文串 )Best Reward -- hdu -- 3613
    Center Alignment
    Chat Server's Outgoing Traffic
  • 原文地址:https://www.cnblogs.com/Love/p/1323718.html
Copyright © 2011-2022 走看看