static async Task ProcessWebRequest(string uri, string method, string load, string onSuccess, string onFailure, string tracking, string contentType, Variable headers, int timeout, bool justFire = false) { try { WebRequest request = WebRequest.CreateHttp(uri); request.Method = method; request.ContentType = contentType; if (!string.IsNullOrWhiteSpace(load)) { var bytes = Encoding.UTF8.GetBytes(load); request.ContentLength = bytes.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); } } if (headers != null && headers.Tuple != null) { var keys = headers.GetKeys(); foreach (var header in keys) { var headerValue = headers.GetVariable(header).AsString(); request.Headers.Add(header, headerValue); } } Task <WebResponse> task = request.GetResponseAsync(); Task finishTask = FinishRequest(onSuccess, onFailure, tracking, task, timeout); if (justFire) { return; } await finishTask; } catch (Exception exc) { await CustomFunction.RunAsync(onFailure, new Variable(tracking), new Variable(""), new Variable(exc.Message)); } }
static void ProcessWebRequest(string uri, string method, string load, string onSuccess, string onFailure, string tracking, string contentType, Variable headers) { try { WebRequest request = WebRequest.CreateHttp(uri); request.Method = method; request.ContentType = contentType; if (!string.IsNullOrWhiteSpace(load)) { var bytes = Encoding.UTF8.GetBytes(load); request.ContentLength = bytes.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); } } if (headers != null && headers.Tuple != null) { var keys = headers.GetKeys(); foreach (var header in keys) { var headerValue = headers.GetVariable(header).AsString(); request.Headers.Add(header, headerValue); } } HttpWebResponse resp = request.GetResponse() as HttpWebResponse; string result; using (StreamReader sr = new StreamReader(resp.GetResponseStream())) { result = sr.ReadToEnd(); } string responseCode = resp == null ? "" : resp.StatusCode.ToString(); CustomFunction.Run(onSuccess, new Variable(tracking), new Variable(responseCode), new Variable(result)); } catch (Exception exc) { CustomFunction.Run(onFailure, new Variable(tracking), new Variable(""), new Variable(exc.Message)); } }