public async Task <RequestResponse> GetResponseAsync(string json, ActionEnum method) { string transportUrl = Url.TransportUrl(method, ConnectionString.Uri); HttpStatusCode Code; RequestResponse result = new RequestResponse(); HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(transportUrl); myHttpWebRequest.Method = "POST"; myHttpWebRequest.ContentType = "application/json"; myHttpWebRequest.CookieContainer = Instance.AuthCookieContainer; Uri siteUri = new Uri(transportUrl); foreach (Cookie cookie in Instance.AuthCookieContainer.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) { //Auth.Add(myHttpWebResponse.Cookies); string val = myHttpWebResponse.Cookies["BPMSESSIONID"].Value; Cookie C = new Cookie("BPMSESSIONID", val); Instance.AuthCookieContainer.Add(new Uri(Instance.ConnectionString.Uri), 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); }
static async Task Main(string[] args) { if (args.Length > 0) { ReadArguments(args); } Utils utils = Utils.Instance; ConsoleWriter.WriteMessage(MessageType.Try, Properties.Resources.buildingMessage); try { utils.ConnectionString = GetConnectionString(); ConsoleWriter.WriteMessage(MessageType.OK, Properties.Resources.succesConnectionMessage); } catch (FileNotFoundException e) { string message = $"{e.Message} File: {e.FileName}"; ConsoleWriter.WriteMessage(MessageType.Error, message); return; } if (utils.ConnectionString != null) { ConsoleWriter.WriteMessage(MessageType.Try, $"Attempting to connect to {utils.ConnectionString.Uri}"); try { utils.Login(); ConsoleWriter.WriteMessage(MessageType.OK, Properties.Resources.loginSuccessMessage); Console.ForegroundColor = ConsoleColor.Yellow; foreach (Cookie cookie in utils.AuthCookieContainer.GetCookies(new Uri(utils.ConnectionString.Uri))) { int length = (cookie.Value.Length > 30) ? 30 : cookie.Value.Length; Console.WriteLine($"\t{cookie.Name}:\t{cookie.Value.Substring(0, length)}"); } Console.WriteLine(); Console.ResetColor(); } catch (ModelBuilderException e) { ConsoleWriter.WriteMessage(MessageType.Error, e.Message); return; } } ConsoleWriter.WriteMessage(MessageType.Try, Properties.Resources.metadataMessage); RequestResponse rr = await utils.GetMetadata().ConfigureAwait(false); await utils.LogoutAsync().ConfigureAwait(false); if (rr.ErrorMessage == null) { Console.ForegroundColor = ConsoleColor.Green; int count = CountXmlLines(rr?.Result ?? ""); ConsoleWriter.WriteMessage(MessageType.OK, $"Obtained definition for {count} entities"); Console.WriteLine($"Would you like to create {count} models? Press any key to continue, <Esc> to exit"); ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.Escape) { ConsoleWriter.WriteMessage(MessageType.Info, Properties.Resources.exitMessage); } else { ConsoleWriter.WriteMessage(MessageType.Info, Properties.Resources.waitMessage); await BuildModels(rr.Result).ConfigureAwait(false); } } else { ConsoleWriter.WriteMessage(MessageType.Error, rr.ErrorMessage); return; } }
static async Task Main() { //RequestResponse rrr = Factory.Create<RequestResponse>(); //rrr.ErrorMessage = ""; //rrr.HttpStatusCode = HttpStatusCode.OK; //rrr.Result = File.ReadAllText(@"C:\metadata.xml"); //await BuildModels(rrr.Result).ConfigureAwait(false); //return; Utils utils = Utils.Instance; ConsoleWriter.WriteMessage(MessageType.Try, Resources.TryMsgBuildConStr); try { utils.ConnectionString = GetConnectionString(); ConsoleWriter.WriteMessage(MessageType.OK, Resources.OkMsgGotCS); } catch (FileNotFoundException e) { ConsoleWriter.WriteMessage(MessageType.Error, $"{e.Message} File: {e.FileName}"); return; } if (utils.ConnectionString != null) { ConsoleWriter.WriteMessage(MessageType.Try, $"Attempting to connect to {utils.ConnectionString.Uri}"); try { utils.Login(); Console.ForegroundColor = ConsoleColor.Green; ConsoleWriter.WriteMessage(MessageType.OK, Resources.OkMsgLoginOk); Console.ForegroundColor = ConsoleColor.Yellow; foreach (Cookie cookie in utils.AuthCookieContainer.GetCookies(new Uri(utils.ConnectionString.Uri))) { int length = (cookie.Value.Length > 30) ? 30 : cookie.Value.Length; //Console.WriteLine($"\t{cookie.Name}:\t{cookie.Value.Substring(0, length)}"); ConsoleWriter.WriteMessage(MessageType.Info, $"\t{cookie.Name}:\t{cookie.Value.Substring(0, length)}"); } Console.WriteLine(); } catch (ModelBuilderException e) { ConsoleWriter.WriteMessage(MessageType.Error, e.Message); Console.WriteLine(); return; } } ConsoleWriter.WriteMessage(MessageType.Try, Resources.TryMsgAttempting); RequestResponse rr = await utils.GetMetadata().ConfigureAwait(false); await utils.LogoutAsync().ConfigureAwait(false); if (rr.ErrorMessage == null) { Console.ForegroundColor = ConsoleColor.Green; int count = CountXmlLines(rr?.Result ?? ""); ConsoleWriter.WriteMessage(MessageType.OK, $"Obtained definition for {count} entities"); Console.WriteLine(); Console.WriteLine($"Would you like to create {count} models? Press any key to continue, <Esc> to exit"); ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.Escape) { Console.WriteLine(Resources.MsgExit); } else { Console.WriteLine(Resources.MsgWait); await BuildModels(rr.Result).ConfigureAwait(false); } } else { ConsoleWriter.WriteMessage(MessageType.Error, rr.ErrorMessage); Console.WriteLine(); return; } }