コード例 #1
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;
                    }
                }

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

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

            lock (pendingRequests)
                pendingRequests.Add(response);
            return(response);
        }
コード例 #2
0
 public static HttpResponse Get(HttpGetRequest request, Action <HttpResponse> action, Dispatcher dispatcher)
 {
     return(Get(request, r => dispatcher.BeginInvoke(() => action(r))));
 }