private static async void InsertNewContact() { Utils utils = Utils.Instance; DirectoryInfo dir = Directory.GetParent(Environment.CurrentDirectory); dir = Directory.GetParent(dir.FullName); dir = Directory.GetParent(dir.FullName); string jsonDir = Path.Combine(dir.FullName, "jsonSamples"); string json = File.ReadAllText(Path.Combine(jsonDir, "InsertNewContact.json")); RequestResponse rr = await utils.ExecuteRequest(json, ActionEnum.INSERT); if (!string.IsNullOrEmpty(rr.ErrorMessage)) { Console.WriteLine(rr.ErrorMessage); } else { Console.WriteLine(rr.Result); } }
private async Task <RequestResponse> GetResponseAsync(string json, ActionEnum method) { string transportUrl = Url.TransportUrl(method, Instance.domain); HttpStatusCode Code; RequestResponse result = new RequestResponse(); HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(transportUrl); myHttpWebRequest.Method = "POST"; myHttpWebRequest.ContentType = "application/json"; myHttpWebRequest.CookieContainer = Instance.Auth; Uri siteUri = new Uri(transportUrl); foreach (Cookie cookie in Instance.Auth.GetCookies(siteUri)) { if (cookie.Name == "BPMCSRF" || cookie.Name == "BPMSESSIONID") { myHttpWebRequest.Headers.Add(cookie.Name, cookie.Value); } } //encode json byte[] postBytes = Encoding.UTF8.GetBytes(json); //Prepare Request Stream using (Stream requestStream = myHttpWebRequest.GetRequestStream()) { requestStream.Write(postBytes, 0, postBytes.Length); } //Send Request try { using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)await myHttpWebRequest.GetResponseAsync().ConfigureAwait(false)) { Code = myHttpWebResponse.StatusCode; /*** * !!! VERY IMPORTANT !!! * READ: https://academy.bpmonline.com/documents/technic-sdk/7-14/executing-odata-queries-using-fiddler * User session is created only upon the first request to the EntityDataService.svc, after which the BPMSESSIONID cookie will be returned in the response. * Therefore, there is no need to add BPMSESSIONID cookie to the title of the first request. * If you do not add BPMSESSIONID cookie to each subseqnent request, then each request will create a new user session. * Significant frequency of requests (several or more requests a minute) will increase the RAM consumption which will decrease performance. */ if (Instance.BpmSessionId == false) { string val = myHttpWebResponse.Cookies["BPMSESSIONID"].Value; Cookie C = new Cookie("BPMSESSIONID", val); Instance.Auth.Add(new Uri(Instance.domain), C); Instance.BpmSessionId = true; } using (StreamReader MyStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream(), true)) { result.HttpStatusCode = Code; result.Result = MyStreamReader.ReadToEnd(); result.ErrorMessage = null; } } } catch (WebException we) { Code = ((HttpWebResponse)(we).Response).StatusCode; using (StreamReader MyStreamReader = new StreamReader(((HttpWebResponse)(we).Response).GetResponseStream(), true)) { result.HttpStatusCode = Code; result.Result = null; result.ErrorMessage = MyStreamReader.ReadToEnd(); } } return(result); }
public async Task <RequestResponse> ExecuteRequest(string jsonRequestBody, ActionEnum requestType) { RequestResponse requestResponse = await GetResponseAsync(jsonRequestBody, requestType); return(requestResponse); }