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

  • 相关阅读:
    线程
    数据类型转换
    复合赋值运算符
    算数运算符
    apache自带的ab压力测试工具用法详解
    创建一个方便快捷实验环境快照
    安装中文版cacti监控华为交换机流量并实现95计费
    虚拟机拷贝后网卡eth0变成了eth1的解决办法
    解压版Tomcat配置
    PLSQL不好用,提示ora-12514 错误解决方法
  • 原文地址:https://www.cnblogs.com/Love/p/1323718.html
Copyright © 2011-2022 走看看