public static HttpResponse Post(HttpPostRequest req, Action<HttpResponse> action) { HttpResponse response = new HttpResponse(req); try { string boundary = ""; HttpWebRequest request = Http.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 (Cookie current in req.Cookies) { request.CookieContainer.Add(request.RequestUri, current); } } request.Method = "POST"; if (req.ContentType != null) { request.ContentType = req.ContentType; } if (req.Header.Count > 0) { foreach (KeyValuePair<string, string> current2 in req.Header) { request.Headers[current2.Key] = current2.Value; } } response.CreateTimeoutTimer(request); request.BeginGetRequestStream(delegate(IAsyncResult ar1) { try { using (Stream stream = request.EndGetRequestStream(ar1)) { if (req.Files.Count > 0) { Http.WritePostData(stream, boundary, req); } else { Http.WritePostData(stream, req); } } request.BeginGetResponse(delegate(IAsyncResult r) { Http.ProcessResponse(r, request, response, action); }, request); } catch (Exception exception2) { response.Exception = exception2; if (action != null) { action(response); } } }, request); } catch (Exception exception) { response.Exception = exception; if (action != null) { action(response); } } lock (Http.pendingRequests) { Http.pendingRequests.Add(response); } return response; }
private static void ProcessResponse(IAsyncResult asyncResult, WebRequest request, HttpResponse resp, Action<HttpResponse> action) { lock (Http.pendingRequests) { if (Http.pendingRequests.Contains(resp)) { Http.pendingRequests.Remove(resp); } } try { WebResponse webResponse = request.EndGetResponse(asyncResult); resp.IsConnected = true; WebResponse webResponse2 = webResponse; using (webResponse) { if (resp.Request.ResponseAsStream) { resp.ResponseStream = webResponse.GetResponseStream(); } else { resp.RawResponse = webResponse.GetResponseStream().ReadToEnd(); } if (webResponse2.Headers.AllKeys.Contains("Set-Cookie")) { string text = webResponse2.Headers["Set-Cookie"]; int num = text.IndexOf(';'); if (num != -1) { foreach (KeyValuePair<string, string> current in HttpUtilityExtensions.ParseQueryString(text.Substring(0, num))) { resp.Cookies.Add(new Cookie(current.Key, current.Value)); } } } } } catch (Exception ex2) { WebException ex = ex2 as WebException; if (ex != null) { HttpWebResponse httpWebResponse = ex.Response as HttpWebResponse; if (httpWebResponse != null) { resp.HttpStatusCode = httpWebResponse.StatusCode; } } if (resp.ResponseStream != null) { resp.ResponseStream.Dispose(); resp.ResponseStream = null; } resp.Exception = ex2; if (action != null) { action(resp); } return; } if (action != null) { action(resp); } }
public static HttpResponse Get(HttpGetRequest req, Action<HttpResponse> action) { HttpResponse response = new HttpResponse(req); try { if (!req.UseCache) { req.Query["__dcachetime"] = DateTime.Now.Ticks.ToString(); } HttpWebRequest request = Http.CreateRequest(req); if (req.Credentials != null) { request.Credentials = req.Credentials; } response.WebRequest = request; if (req.Cookies.Count > 0) { request.CookieContainer = new CookieContainer(); foreach (Cookie current in req.Cookies) { request.CookieContainer.Add(request.RequestUri, current); } } request.Method = "GET"; if (req.ContentType != null) { request.ContentType = req.ContentType; } if (req.Header.Count > 0) { foreach (KeyValuePair<string, string> current2 in req.Header) { request.Headers[current2.Key] = current2.Value; } } response.CreateTimeoutTimer(request); request.BeginGetResponse(delegate(IAsyncResult r) { Http.ProcessResponse(r, request, response, action); }, request); } catch (Exception exception) { response.Exception = exception; if (action != null) { action(response); } } lock (Http.pendingRequests) { Http.pendingRequests.Add(response); } return response; }