} // End Function GetFormValues public static void Test() { string projName = RedmineMailService.SecretManager.GetSecret <string>("SAP_Project_Name"); string url = RedmineMailService.SecretManager.GetSecret <string>("URL", projName); string username = RedmineMailService.SecretManager.GetSecret <string>("Username", projName); string password = RedmineMailService.SecretManager.GetSecret <string>("Password", projName); string base64Creds = System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(username + ":" + password)); string basicCredentials = "Basic " + base64Creds; using (WebClientEx wc = new WebClientEx()) { wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"); wc.Headers.Add("Authorization", basicCredentials); // Request 1 string redirect = wc.DownloadString(url); RedirectValues rv1 = GetFormValues(redirect); byte[] retValue = wc.UploadValues(rv1.Action, rv1.Method, rv1.PostValues); string resp = System.Text.Encoding.UTF8.GetString(retValue); System.Console.WriteLine(resp); RedirectValues rv2 = GetFormValues(resp); System.Console.WriteLine(rv2); // Request 2 wc.Headers["referer"] = url; rv2.PostValues["j_username"] = username; rv2.PostValues["j_password"] = password; // string url2 = "https://accounts.sap.com" + rv2.Action; System.Uri baseUri = new System.Uri(rv1.Action, System.UriKind.Absolute); string url2 = baseUri.Scheme + "://" + baseUri.Authority + rv2.Action; byte[] loginRetValue = wc.UploadValues(url2, rv2.Method, rv2.PostValues); string strLoginRetValue = System.Text.Encoding.UTF8.GetString(loginRetValue); System.Console.WriteLine(strLoginRetValue); System.Console.WriteLine(wc.CookieContainer); RedirectValues rv3 = GetFormValues(strLoginRetValue); System.Console.WriteLine(rv3); // Request 3 wc.Headers["referer"] = url2; byte[] retValueRedirect = wc.UploadValues(rv3.Action, rv3.Method, rv3.PostValues); string strRedirectResult = System.Text.Encoding.UTF8.GetString(retValueRedirect); System.Console.WriteLine(strRedirectResult); } // End Using wc } // End Sub Test
} // End Class RedirectValues public static RedirectValues GetFormValues(string html) { RedirectValues rv = new RedirectValues(); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); HtmlAgilityPack.HtmlNode form = doc.DocumentNode.SelectSingleNode("//form"); rv.Method = form.Attributes["method"]?.Value; rv.Action = form.Attributes["action"]?.Value; foreach (HtmlAgilityPack.HtmlNode node in form.SelectNodes(".//input")) { HtmlAgilityPack.HtmlAttribute valueAttribute = node.Attributes["value"]; HtmlAgilityPack.HtmlAttribute nameAttribute = node.Attributes["name"]; if (nameAttribute != null && valueAttribute != null) { rv.PostValues.Add(nameAttribute.Value, valueAttribute.Value); } // End if (nameAttribute != null && valueAttribute != null) } // Next node return(rv); } // End Function GetFormValues