private void Login() { Lib.iojson o = new Lib.iojson(); Lib.iojson i = new Lib.iojson(); LoginInfo li = new LoginInfo() { Username = ConfigurationManager.AppSettings["Username"], Password = ConfigurationManager.AppSettings["Password"], }; o.AddObjToArr(li); i = Lib.Util.PostWebContent(ref this.httpdata, "/Login", o.Encode()); if (i.Status != true) { MessageBox.Show(String.Join("; ", i.ErrArr)); } else { // Get the first CSRF token. i = Lib.Util.GetWebContent(ref this.httpdata, "/CSRFToken"); if (i.Status != true) { MessageBox.Show(String.Join("; ", i.ErrArr)); } else { SrvReqBtn.IsEnabled = true; } } }
public static iojson GetWebContent(ref HTTPData httpdata, string url) { iojson i = new iojson(); try { Debug.WriteLine("GetWebContent.url: " + url); // NOTE: this is a temporary remove SSL ceritficate check solution. Please comment out this line in production. if (ConfigurationManager.AppSettings["IsFakeSSLCert"] == "yes") { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); } var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, CookieContainer = httpdata.Cookie, }); client.BaseAddress = new Uri(GetWebSrvURL()); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync(url).Result; // will throw an exception if not successful response.EnsureSuccessStatusCode(); string resultContent = response.Content.ReadAsStringAsync().Result; // set CSRF token IEnumerable <string> values; if (response.Headers.TryGetValues("X-CSRF-Token", out values)) { httpdata.CSRFtoken = values.First(); } i.Decode(resultContent); } catch (Exception e) { // TODO: need to improve. Debug.WriteLine("GetWebContent: " + e.Message.ToString()); i.AddError("GetWebContent: " + e.Message.ToString()); } Debug.WriteLine("GetWebContent.Encode: " + i.Encode()); return(i); }