Ejemplo n.º 1
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;
                    }
                }

                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 ex)
                    {
                        response.Exception = ex;
                        if (action != null)
                            action(response);
                    }
                }, request);
            }
            catch (System.Exception ex)
            {
                response.Exception = ex;
                if (action != null)
                {
                    action(response);
                }
            }

            lock (pendingRequests)
            {
                pendingRequests.Add(response);
            }

            return response;
        }
Ejemplo n.º 2
0
        private static void ProcessResponse(IAsyncResult asyncResult, WebRequest request, HttpResponse resp, Action<HttpResponse> action)
        {
            lock (pendingRequests)
            {
                if (pendingRequests.Contains(resp))
                {
                    pendingRequests.Remove(resp);
                }
            }

            try
            {
                var response = request.EndGetResponse(asyncResult);
                resp.IsConnected = true;
                var origResponse = response;

                using (response)
                {
                    if (resp.Request.ResponseAsStream)
                    {
                        resp.ResponseStream = response.GetResponseStream();
                    }
                    else
                    {
                        resp.RawResponse = response.GetResponseStream().ReadToEnd();
                    }
                    if (origResponse.Headers.AllKeys.Contains("Set-Cookie"))
                    {
                        var cookies = origResponse.Headers["Set-Cookie"];
                        var index = cookies.IndexOf(';');
                        if (index != -1)
                        {
                            foreach (var c in ParseQueryString(cookies.Substring(0, index)))
                            {
                                resp.Cookies.Add(new Cookie(c.Key, c.Value));
                            }
                        }
                    }

                    foreach (var key in origResponse.Headers.AllKeys)
                    {
                        var value = origResponse.Headers[key];
                        resp.Headers.Add(key, value);
                    }
                }
            }
            catch (System.Exception ex)
            {
                var we = ex as WebException;
                if (we != null)
                {
                    var temp = we.Response as HttpWebResponse;
                    if (temp != null)
                    {
                        resp.HttpStatusCode = temp.StatusCode;
                    }
                }

                if (resp.ResponseStream != null)
                {
                    resp.ResponseStream.Dispose();
                    resp.ResponseStream = null;
                }

                resp.Exception = ex;
                if (action != null)
                {
                    action(resp);
                }
                return;
            }
            if (action != null)
            {
                action(resp);
            }
        }
Ejemplo n.º 3
0
        public static HttpResponse Get(HttpGetRequest req, Action<HttpResponse> action)
        {
            var response = new HttpResponse(req);
            try
            {
                if (!req.UseCache)
                {
                    req.Query["__dcachetime"] = DateTime.Now.Ticks.ToString();
                }

                var request = CreateRequest(req);

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

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

                request.Method = "GET";
                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;
                    }
                }

                response.CreateTimeoutTimer(request);
                request.BeginGetRequestStream(r => ProcessResponse(r, request, response, action), request);
            }
            catch (Exception ex)
            {
                response.Exception = ex;
                if (action != null)
                {
                    action(response);
                }
            }

            lock (pendingRequests)
            {
                pendingRequests.Add(response);
            }

            return response;
        }