/// <summary> /// loads a document using the browser /// </summary> /// <param name="address"></param> /// <param name="parameters"></param> /// <returns></returns> public HtmlDocument LoadDocument(string address, WebRequestParameters parameters = null) { BrowserResponse response = browser.Load(address, parameters); using (response.Response) { HtmlDocument document = new HtmlDocument(); document.Load(response.Response, response.Encoding); return(document); } }
/// <summary> /// loads data of an uri /// </summary> /// <param name="address">address from which to load document</param> /// <param name="parameters">request parameters</param> /// <returns></returns> public BrowserResponse Load(string address, WebRequestParameters parameters = null) { if (parameters?.Query != null) { address += "?" + CreateUriParameters(parameters.Query); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(address)); request.AllowAutoRedirect = false; request.PreAuthenticate = true; request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested; request.CookieContainer = cookies; request.Method = parameters?.FormData != null ? "POST" : "GET"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; request.Headers.Add("Accept-Language", "de,en-US;q=0.7,en;q=0.3"); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; //request.Timeout = 60000; if (parameters != null) { PrepareParameters(request, parameters); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); switch (response.StatusCode) { case HttpStatusCode.OK: break; case HttpStatusCode.Found: string location = response.Headers.Get("Location"); if (OnRedirect(location)) { // close response or redirect could freeze response.Close(); Uri target = Uri.IsWellFormedUriString(location, UriKind.Absolute) ? new Uri(location) : new Uri(response.ResponseUri, location); return(Load(target.AbsoluteUri)); } break; default: throw new WebException("Unable to load document"); } Encoding encoding = DetectEncoding(response); return(new BrowserResponse(response.GetResponseStream(), encoding)); }
/// <summary> /// simulates a browser load /// </summary> /// <param name="address">address to load</param> /// <param name="parameters">parameters for request</param> /// <returns>response of browser</returns> public BrowserResponse Load(string address, WebRequestParameters parameters = null) { SimulationResponse response = datafunc(address, parameters); if (!string.IsNullOrEmpty(response.RedirectionTarget)) { if (Redirect?.Invoke(response.RedirectionTarget) ?? false) { return(Load(response.RedirectionTarget)); } } return(new BrowserResponse(response.ResponseData, response.ResponseEncoding)); }
void PrepareParameters(HttpWebRequest request, WebRequestParameters parameters) { if (parameters.Credentials != null) { request.Credentials = parameters.Credentials; } if (parameters.Referer != null) { request.Referer = parameters.Referer; } if (parameters.FormData == null) { return; } request.ContentType = "application/x-www-form-urlencoded"; using (StreamWriter sw = new StreamWriter(request.GetRequestStream())) sw.Write(CreateUriParameters(parameters.FormData)); }