public static Task<HttpResponse> GetAsync(HttpGetRequest request) { var task = new TaskCompletionSource<HttpResponse>(); Get(request, result => { if (result.Successful) { task.SetResult(result); } else if (result.Canceled) { task.SetCanceled(); } else { task.SetException(result.Exception); } }); return task.Task; }
private static HttpWebRequest CreateRequest(HttpGetRequest req) { var queryString = GetQueryString(req.Query); HttpWebRequest request = null; if (string.IsNullOrEmpty(queryString)) { request = (HttpWebRequest)WebRequest.Create(req.Uri.AbsoluteUri); } else if (req.Uri.AbsoluteUri.Contains(":")) { request = (HttpWebRequest)WebRequest.Create(req.Uri.AbsoluteUri + "&" + queryString); } else { request = (HttpWebRequest)WebRequest.Create(req.Uri.AbsoluteUri + "?" + queryString); } return request; }
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; }