110,534
社区成员
发帖
与我相关
我的任务
分享
post.AppendFormat("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);
post.AppendFormat("\r\n--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);
String contenttype = "image/pjpeg";
//string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
try
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
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(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(fileToUpload));
//sb.Append(fileToUpload);
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n");
//string postHeader = sb.ToString() + postDateAdd;
string postData = "";
if (postDateAdd != "")
{
StringBuilder post = new StringBuilder();
string[] array = postDateAdd.Split('&');
foreach (string str in array)
{
string[] arr = str.Split('=');
post.AppendFormat("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, arr[0], arr[1]);
}
post.AppendFormat("--{0}--\r\n", boundary);
postData = post.ToString();
}
string postHeader = sb.ToString();
byte[] postHeaderBytes = encoding.GetBytes(postHeader);
byte[] postDateAddBytes = encoding.GetBytes(postData);
//byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
//long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length + postDateAddBytes.Length;
long length = postHeaderBytes.Length + fileStream.Length + postDateAddBytes.Length;
webrequest.ContentLength = length;
webrequest.CookieContainer = cc;
webrequest.AllowAutoRedirect = true;
webrequest.ServicePoint.ConnectionLimit = maxTry;
webrequest.Referer = uploadUrl;
webrequest.Accept = accept;
webrequest.UserAgent = userAgent;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new Byte[(int)fileStream.Length];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
//requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Write(postDateAddBytes, 0, postDateAddBytes.Length);
HttpWebResponse res = (HttpWebResponse)webrequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), encoding);
string html = sr.ReadToEnd();
requestStream.Close();
return html;