日期:2014-05-18 浏览次数:21024 次
string boundary = "--------" + DateTime.Now.Ticks.ToString(); HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri); webrequest.CookieContainer = cookies; webrequest.KeepAlive = true; webrequest.ProtocolVersion = HttpVersion.Version10; webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; webrequest.Accept = "text/html, */*"; webrequest.UserAgent = "Mozilla/3.0 (compatible; Indy Library)"; StringBuilder sb = new StringBuilder(); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"safeCode\"");\\此参数无法接收到 sb.Append("\r\n"); sb.Append("Content-Type: text/plain"); sb.Append("\r\n"); sb.Append("Content-Transfer-Encoding: quoted-printable"); sb.Append("\r\n"); sb.Append("\r\n"); sb.Append("48555"); sb.Append("\r\n"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"filePath\"");\\此参数无法接收到 sb.Append("\r\n"); sb.Append("Content-Type: text/plain"); sb.Append("\r\n"); sb.Append("Content-Transfer-Encoding: quoted-printable"); sb.Append("\r\n"); sb.Append("\r\n"); sb.Append("/1/2012/5/767/767_O.docx"); sb.Append("\r\n"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"files\";");\\此参数无法接收到 sb.Append("filename=\""); sb.Append("767_O.docx.jxup"); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: application/octet-stream"); sb.Append("\r\n"); sb.Append("Content-Transfer-Encoding: binary"); sb.Append("\r\n"); sb.Append("\r\n"); string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n----------" + boundary + "\r\n"); FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; webrequest.ContentLength = length; Stream requestStream = webrequest.GetRequestStream(); requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); byte[] buffer = new Byte[checked((uint)Math.Min(32768, (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); webrequest.Timeout = 1000000; WebResponse responce = webrequest.GetResponse(); Stream s = responce.GetResponseStream(); StreamReader sr = new StreamReader(s); string str = sr.ReadToEnd(); fileStream.Close(); requestStream.Close(); sr.Close(); s.Close(); responce.Close();