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