Example #1
0
 public FileInfo ToFileInfo(Stream stream, string paramName, string mimeType = "application/octet-stream")
 {
     return(new FileInfo(paramName, StreamHelper.ReadAsBytes(stream), mimeType));
 }
Example #2
0
        private WebRequest PrepareRequest(string path, string method, Dictionary <string, object> formParams,
                                          Dictionary <string, string> headerParams, string body, string contentType)
        {
            var client = WebRequest.Create(path);

            client.Method = method;

            byte[] formData = null;
            if (formParams.Count > 0)
            {
                if (formParams.Count > 1)
                {
                    string formDataBoundary = "Somthing";
                    client.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
                    formData           = GetMultipartFormData(formParams, formDataBoundary);
                }
                else
                {
                    client.ContentType = "multipart/form-data";
                    formData           = GetMultipartFormData(formParams, string.Empty);
                }

                client.ContentLength = formData.Length;
            }
            else
            {
                client.ContentType = contentType;
            }

            foreach (var headerParamsItem in headerParams)
            {
                client.Headers.Add(headerParamsItem.Key, headerParamsItem.Value);
            }

            foreach (var defaultHeaderMapItem in this.defaultHeaderMap)
            {
                if (!headerParams.ContainsKey(defaultHeaderMapItem.Key))
                {
                    client.Headers.Add(defaultHeaderMapItem.Key, defaultHeaderMapItem.Value);
                }
            }

            MemoryStream streamToSend = null;

            try
            {
                switch (method)
                {
                case "GET":
                    break;

                case "POST":
                case "PUT":
                case "DELETE":
                    streamToSend = new MemoryStream();

                    if (formData != null)
                    {
                        streamToSend.Write(formData, 0, formData.Length);
                    }

                    if (body != null)
                    {
                        var requestWriter = new StreamWriter(streamToSend);
                        requestWriter.Write(body);
                        requestWriter.Flush();
                    }

                    break;

                default:
                    throw new ApiException("unknown method type " + method, StatusCodes.InternalError);
                }

                this.requestHandlers.ForEach(p => p.BeforeSend(client, streamToSend));

                if (streamToSend != null)
                {
                    using (Stream requestStream = client.GetRequestStream())
                    {
                        StreamHelper.CopyTo(streamToSend, requestStream);
                    }
                }
            }
            finally
            {
                if (streamToSend != null)
                {
                    streamToSend.Dispose();
                }
            }

            return(client);
        }