Beispiel #1
0
        public static Task <HttpResponse> PostAsync(HttpPostRequest request)
        {
            var task = new TaskCompletionSource <HttpResponse>();

            Post(request, result =>
            {
                if (result.Successful)
                {
                    task.SetResult(result);
                }
                else if (result.Canceled)
                {
                    task.SetCanceled();
                }
                else
                {
                    task.SetException(result.Exception);
                }
            });
            return(task.Task);
        }
Beispiel #2
0
        private static void WritePostData(Stream stream, String boundary, HttpPostRequest request)
        {
            var boundarybytes = request.Encoding.GetBytes("\r\n--" + boundary + "\r\n");

            const string dataHeaderTemplate = "Content-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

            if (request.RawData != null)
            {
                throw new Exception("RawData not allowed if uploading files");
            }

            foreach (var tuple in request.Data)
            {
                stream.Write(boundarybytes, 0, boundarybytes.Length);

                var formitem      = string.Format(dataHeaderTemplate, tuple.Key, tuple.Value);
                var formitembytes = request.Encoding.GetBytes(formitem);
                stream.Write(formitembytes, 0, formitembytes.Length);
            }

            const string fileHeaderTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

            foreach (var file in request.Files)
            {
                stream.Write(boundarybytes, 0, boundarybytes.Length);

                var header      = string.Format(fileHeaderTemplate, file.Name, file.Filename, file.ContentType ?? "application/octet-stream");
                var headerbytes = request.Encoding.GetBytes(header);
                stream.Write(headerbytes, 0, headerbytes.Length);

                var fileStream = file.Stream;

#if WINRT
                if (fileStream == null)
                {
                    var operation = file.File.OpenReadAsync();
                    var task      = operation.AsTask();
                    task.RunSynchronously();
                    fileStream = operation.GetResults().AsStreamForRead();
                }
#else
#if SL4 || SL5 || WP7 || WP8
                if (fileStream == null)
                {
                    var isf = IsolatedStorageFile.GetUserStoreForApplication();
                    fileStream = new IsolatedStorageFileStream(file.Path, FileMode.Open, FileAccess.Read, isf);
                }
#else
                if (fileStream == null)
                {
                    fileStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read);
                }
#endif
#endif

                try
                {
                    var buffer    = new byte[1024];
                    var bytesRead = 0;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                        //stream.Flush();
                        // TODO call progress changed event handler (calculate progress over all files) => use flush to have correct behaviour
                        // TODO add progress for download
                    }
                }
                finally
                {
                    if (file.CloseStream)
                    {
                        fileStream.Dispose();
                    }
                }
            }

            boundarybytes = request.Encoding.GetBytes("\r\n--" + boundary + "--\r\n");
            stream.Write(boundarybytes, 0, boundarybytes.Length);
        }
Beispiel #3
0
        private static void WritePostData(Stream stream, HttpPostRequest request)
        {
            var bytes = request.RawData ?? request.Encoding.GetBytes(GetQueryString(request.Data));

            stream.Write(bytes, 0, bytes.Length);
        }
Beispiel #4
0
        private static HttpResponse Post(HttpPostRequest req, Action <HttpResponse> action, string method)
        {
            var response = new HttpResponse(req);

            try
            {
                var boundary = "";
                var request  = CreateRequest(req);

                if (req.Credentials != null)
                {
                    request.Credentials = req.Credentials;
                }
                response.WebRequest = request;

                if (req.Files.Count == 0)
                {
                    request.ContentType = "application/x-www-form-urlencoded";
                }
                else
                {
                    boundary            = "----------------------------" + DateTime.Now.Ticks.ToString("x");
                    request.ContentType = "multipart/form-data; boundary=" + boundary;
                }

                if (req.Cookies.Count > 0)
                {
                    request.CookieContainer = new CookieContainer();
                    foreach (var c in req.Cookies)
                    {
                        request.CookieContainer.Add(request.RequestUri, c);
                    }
                }

                request.Method = method;
                if (req.ContentType != null)
                {
                    request.ContentType = req.ContentType;
                }

                if (req.Headers.Count > 0)
                {
                    foreach (var item in req.Headers)
                    {
                        request.Headers[item.Key] = item.Value;
                    }
                }

#if USE_GZIP
                if (req.RequestGzip)
                {
                    request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
                }
#endif

                response.CreateTimeoutTimer(request);
                request.BeginGetRequestStream(delegate(IAsyncResult ar1)
                {
                    try
                    {
                        using (var stream = request.EndGetRequestStream(ar1))
                        {
                            if (req.Files.Count > 0)
                            {
                                WritePostData(stream, boundary, req);
                            }
                            else
                            {
                                WritePostData(stream, req);
                            }
                        }

                        request.BeginGetResponse(r => ProcessResponse(r, request, response, action), request);
                    }
                    catch (Exception e)
                    {
                        response.Exception = e;
                        if (action != null)
                        {
                            action(response);
                        }
                    }
                }, request);
            }
            catch (Exception e)
            {
                response.Exception = e;
                if (action != null)
                {
                    action(response);
                }
            }

            lock (pendingRequests)
                pendingRequests.Add(response);
            return(response);
        }
Beispiel #5
0
 public static HttpResponse Delete(HttpPostRequest req, Action <HttpResponse> action)
 {
     return(Post(req, action, "DELETE"));
 }
Beispiel #6
0
 public static HttpResponse Put(HttpPostRequest req, Action <HttpResponse> action)
 {
     return(Post(req, action, "PUT"));
 }
Beispiel #7
0
 public static HttpResponse Post(HttpPostRequest req, Action <HttpResponse> action, Dispatcher dispatcher)
 {
     return(Post(req, r => dispatcher.BeginInvoke(() => action(r))));
 }