private HtmlFormInfo GetHtmlFormInfo(string htmlContent) { HtmlDocument document = new HtmlDocument(); document.LoadHtml(htmlContent); HtmlNode formNode = document.DocumentNode.SelectSingleNode("//form"); var method = formNode.GetAttributeValue("method", string.Empty); var action = formNode.GetAttributeValue("action", string.Empty); var htmlFormInfo = new HtmlFormInfo(method, action); HtmlNodeCollection inputNodes = formNode.SelectNodes("//input"); foreach (var inputNode in inputNodes) { var name = inputNode.GetAttributeValue("name", string.Empty); var value = inputNode.GetAttributeValue("value", string.Empty); if (!string.IsNullOrEmpty(name)) htmlFormInfo.Parameters.Add(name, value); } return htmlFormInfo; }
private WebRequest CreateWebRequestFromFormInfo(HtmlFormInfo formInfo) { string data = string.Join("&", formInfo.Parameters.Select(parameter => string.Format("{0}={1}", parameter.Key, parameter.Value))); //replace <value> byte[] dataStream = Encoding.UTF8.GetBytes(data); var webRequest = (HttpWebRequest)WebRequest.Create(formInfo.Action); webRequest.Method = formInfo.Method; webRequest.AllowAutoRedirect = false; webRequest.ContentType = "text/html; charset=utf-8"; webRequest.ContentLength = dataStream.Length; using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(dataStream, 0, dataStream.Length); } return webRequest; }