Example #1
0
        /**
         * 写入multipart参数
         *
         * @param boundary
         * @param paramers
         * @param out
         * @throws IOException
         */
        private static void WriteMutiContent(string boundary, HttpParamers paramers, ref Stream stream)
        {
            byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            // 写入文本请求参数
            foreach (KeyValuePair <string, string> paramer in paramers.Paramers)
            {
                byte[] textBytes = GetTextEntry(paramer.Key, paramer.Value);
                stream.Write(boundaryBytes, 0, boundaryBytes.Length);
                stream.Write(textBytes, 0, textBytes.Length);
            }

            // 写入文件请求参数
            if (paramers.FileStreams.Count > 0)
            {
                foreach (KeyValuePair <string, IFileItem> file in paramers.FileStreams)
                {
                    IFileItem fileItem = file.Value;
                    if (!fileItem.IsValid())
                    {
                        throw new Exception("无效的文件流");
                    }

                    byte[] fileBytes = GetFileEntry(file.Key, fileItem.GetFileName(), fileItem.GetMimeType());
                    stream.Write(boundaryBytes, 0, boundaryBytes.Length);
                    stream.Write(fileBytes, 0, fileBytes.Length);
                    fileItem.Write(ref stream);
                }
            }

            //写入批量文件请求
            if (paramers.Files.Count > 0)
            {
                foreach (KeyValuePair <string, List <IFileItem> > listEntry in paramers.Files)
                {
                    List <IFileItem> items = listEntry.Value;
                    foreach (IFileItem item in items)
                    {
                        if (!item.IsValid())
                        {
                            throw new Exception("无效的文件流");
                        }

                        byte[] fileBytes = GetFileEntry(listEntry.Key, item.GetFileName(), item.GetMimeType());
                        stream.Write(boundaryBytes, 0, boundaryBytes.Length);
                        stream.Write(fileBytes, 0, fileBytes.Length);
                        item.Write(ref stream);
                    }
                }
            }


            // 添加请求结束标志
            byte[] endBoundary = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            stream.Write(endBoundary, 0, endBoundary.Length);
        }
Example #2
0
 private static void WriteMutiContent(string boundary, HttpParamers paramers, ref Stream stream)
 {
     byte[] bytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
     foreach (KeyValuePair <string, string> pair in paramers.Paramers)
     {
         byte[] textEntry = GetTextEntry(pair.Key, pair.Value);
         stream.Write(bytes, 0, bytes.Length);
         stream.Write(textEntry, 0, textEntry.Length);
     }
     if (paramers.FileStreams.Count > 0)
     {
         foreach (KeyValuePair <string, IFileItem> pair2 in paramers.FileStreams)
         {
             IFileItem item = pair2.Value;
             if (!item.IsValid())
             {
                 throw new Exception("无效的文件流");
             }
             byte[] buffer4 = GetFileEntry(pair2.Key, item.GetFileName(), item.GetMimeType());
             stream.Write(bytes, 0, bytes.Length);
             stream.Write(buffer4, 0, buffer4.Length);
             item.Write(ref stream);
         }
     }
     if (paramers.Files.Count > 0)
     {
         foreach (KeyValuePair <string, List <IFileItem> > pair3 in paramers.Files)
         {
             foreach (IFileItem item2 in pair3.Value)
             {
                 if (!item2.IsValid())
                 {
                     throw new Exception("无效的文件流");
                 }
                 byte[] buffer5 = GetFileEntry(pair3.Key, item2.GetFileName(), item2.GetMimeType());
                 stream.Write(bytes, 0, bytes.Length);
                 stream.Write(buffer5, 0, buffer5.Length);
                 item2.Write(ref stream);
             }
         }
     }
     byte[] buffer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
     stream.Write(buffer, 0, buffer.Length);
 }