void ParseMultiPart(string contentType, byte[] buffer, System.Text.Encoding encoding) { int boundaryIndex = contentType.IndexOf("boundary=", System.StringComparison.OrdinalIgnoreCase); if (boundaryIndex == -1) { return; } _form = new System.Collections.Specialized.NameValueCollection(); var files = new HttpFileCollection(); _files = files; string boundary = contentType.Substring(boundaryIndex + "boundary=".Length); byte[] spliter = System.Text.Encoding.ASCII.GetBytes(boundary + "\r\nContent-Disposition: form-data; name=\""); //byte[] endFlags = System.Text.Encoding.ASCII.GetBytes(boundary + "--"); bool ended = false; int index = 0; int endIndex = -1; while (!ended && index < buffer.Length - 1) { index += spliter.Length + 2; endIndex = BinarySearch(buffer, index, 34); if (endIndex == -1) { break; } string name = encoding.GetString(buffer, index, endIndex - index); index = endIndex + 1; bool isFile = false; string filename = null; string contentTypeHeader = null; while (true) { if (buffer[index] == 59) //; 还有其它值 { index += 2; endIndex = BinarySearch(buffer, index, 61);//= if (endIndex == -1) { ended = true; break; } string name2 = encoding.GetString(buffer, index, endIndex - index); index = endIndex + 2;//=" endIndex = BinarySearch(buffer, index, 34); if (endIndex == -1) { ended = true; break; } string value2 = encoding.GetString(buffer, index, endIndex - index); index = endIndex + 1; //\r //Console.WriteLine("{0}=[{1}]", name2, value2); if (string.Equals(name2, "filename", System.StringComparison.OrdinalIgnoreCase)) { isFile = true; filename = value2; } } else { break; } } if (ended) { break; } index++; //\n if (buffer[index + 1] != 13) //headers { index++; endIndex = BinarySearch(buffer, index, 58);//: if (endIndex == -1) { break; } string headerName = System.Text.Encoding.ASCII.GetString(buffer, index, endIndex - index); index = endIndex + 2; endIndex = BinarySearch(buffer, index, 13);//\r if (endIndex == -1) { break; } string headerValue = System.Text.Encoding.ASCII.GetString(buffer, index, endIndex - index); index = endIndex + 1;//\r if (string.Equals(headerName, "Content-Type", System.StringComparison.OrdinalIgnoreCase)) { contentTypeHeader = headerValue; } } index += 3; if (isFile) { endIndex = BinarySearch(buffer, index, 13, new byte[] { 13, 10, 45, 45, 45, 45 }); } else { endIndex = BinarySearch(buffer, index, 13, new byte[] { 13, 10 }); } if (endIndex == -1) { break; } int valueLength = endIndex - index; if (isFile) { _form.Add(name, filename); System.IO.MemoryStream inputStream = null; if (valueLength > 0) { inputStream = new System.IO.MemoryStream(valueLength); inputStream.Position = 0; inputStream.Write(buffer, index, valueLength); inputStream.Position = 0; } if (string.IsNullOrEmpty(contentType)) { contentType = "application/octet-stream"; } HttpPostedFile file = new HttpPostedFile(inputStream, filename, valueLength, contentTypeHeader); files.Add(name, file); } else { byte[] valueBuffer = new byte[valueLength]; System.Array.Copy(buffer, index, valueBuffer, 0, valueLength); _form.Add(name, encoding.GetString(valueBuffer)); } index = endIndex + 2; } }