private void WriteBodyAndFiles(Stream stream, B body, List <FileInfo> files, ref string contentType)
 {
     //content is multipart if there are multiple files and/or body params
     if (files.Count + (body != null ? 1 : 0) > 1)
     {
         string formDataBoundary = Guid.NewGuid().ToString();
         int    partIndex        = 0;
         contentType = "multipart/form-data; boundary=" + formDataBoundary;
         if (body != null)
         {
             WriteMultipartFormDataHeader(stream, null, contentType, formDataBoundary, ref partIndex);
             CopyToStream(body, stream);
         }
         int i = 1;
         foreach (FileInfo file in files)
         {
             string fileName = file.Name ?? "file" + i;
             string fileType = file.MimeType ?? "application/octet-stream";
             WriteMultipartFormDataHeader(stream, fileName, fileType, formDataBoundary, ref partIndex);
             StreamHelper.CopyTo(file.Content, stream);
             i++;
         }
         // Add the end of the request.  Start with a newline
         WriteToStream(stream, "\r\n--" + formDataBoundary + "--\r\n");
     }
     else if (body != null)
     {
         CopyToStream(body, stream);
     }
     else if (files.Count > 0)
     {
         StreamHelper.CopyTo(files[0].Content, stream);
     }
 }
Exemplo n.º 2
0
        private void WriteBodyAndFiles(Stream stream, B body, List <FileInfo> files, ref string contentType)
        {
            string formDataBoundary = Guid.NewGuid().ToString();
            int    partCount        = files.Count + (body != null ? 1 : 0);
            int    partIndex        = 0;

            if (files.Count > 0)
            {
                contentType = partCount > 1 ? "multipart/form-data; boundary=" + formDataBoundary : "multipart/form-data";
            }
            if (body != null)
            {
                WriteMultipartFormDataHeader(stream, null, contentType, formDataBoundary, ref partIndex, partCount);
                m_streamCopier.CopyToStream(body, stream);
            }
            foreach (FileInfo file in files)
            {
                WriteMultipartFormDataHeader(stream, file.Name, file.MimeType, formDataBoundary, ref partIndex, partCount);
                StreamHelper.CopyTo(file.Content, stream);
            }
            if (partCount > 1)
            {
                // Add the end of the request.  Start with a newline
                WriteToStream(stream, "\r\n--" + formDataBoundary + "--\r\n");
            }
        }
        private Stream Call(
            string path,
            string method,
            Dictionary <string, string> headerParams,
            List <FileInfo> files,
            B body,
            string contentType)
        {
            WebRequest client = WebRequest.Create(path);

            foreach (var headerParamsItem in headerParams)
            {
                client.Headers[headerParamsItem.Key] = headerParamsItem.Value;
            }
            client.Method = method;
            switch (method)
            {
            case "GET":
                m_requestHandlers.ForEach(p => p.BeforeSend(client, null));
                break;

            case "POST":
            case "PUT":
            case "DELETE":
#if NETFRAMEWORK
                using (Stream requestStream = client.GetRequestStream())
#else
                using (Stream requestStream = client.GetRequestStreamAsync().Result)
#endif
                {
                    WriteBodyAndFiles(requestStream, body, files, ref contentType);
                    client.ContentType = contentType;
                    m_requestHandlers.ForEach(p => p.BeforeSend(client, null));
                }
                break;

            default:
                throw new ApiException(500, "unknown method type " + method);
            }

            HttpWebResponse webResponse = (HttpWebResponse)GetResponse(client);

            MemoryStream resultStream = new MemoryStream();
            StreamHelper.CopyTo(webResponse.GetResponseStream(), resultStream);
            m_requestHandlers.ForEach(p => p.ProcessResponse(webResponse, resultStream));
            resultStream.Position = 0;
            return(resultStream);
        }