public IPttRequest Clone(PttWebRequest pttWebRequest) { var result = new PttRequest(Url); result.PostValue = PostValue; result.Chunked = Chunked; result.CookieContainer = CookieContainer; result.Proxy = Proxy; result.WrappedRequest = pttWebRequest.Convert(); GC.Collect();//onceki request i memory den ucur result.RequestType = RequestType; result.EventValidationValue = EventValidationValue; result.ViewStateValue = ViewStateValue; result.SessionJar = SessionJar; return result; }
public override string GetHtml(IPttRequest request, bool forHotelList) { var proxy = WebHelper.GetProxyForOperator(_name);//proxy si ayarli geliyor olmasi lazim aslinda var lastCaptchaUrl = ""; var result = ""; var th = new Thread(() => { if (proxy != null) { SetSessionProxy(proxy.Item1 + ":" + proxy.Item2, ""); } using (WebBrowser wb = new WebBrowser()) { var redirected = false; wb.DocumentCompleted += (sndr, e) => { if (redirected) { Logger.LogProcess("document completed for " + e.Url + ", redirected"); result = wb.DocumentText; Logger.LogProcess("result for "+ request.Url + ":" + result); Application.ExitThread(); } else//captcha? { Logger.LogProcess("document completed for " + e.Url + " not redirected:"); if (_captchaRequestResponse.CaptchaShown(wb.DocumentText)) { EventHandler handler2 = (sender, eventArgs) => { HtmlElement div = wb.Document.GetElementById("recaptcha_widget_div"); if (div == null) return; var contentLoaded = div.InnerHtml; // get the content loaded via ajax if (string.IsNullOrEmpty(contentLoaded)) return; var htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(contentLoaded); var captchaImage=htmlDoc.DocumentNode.SelectSingleNode("//img[@id='recaptcha_challenge_image']"); if (captchaImage == null) return; var captchaUrl = captchaImage.Attributes["src"].Value; if (lastCaptchaUrl == captchaUrl) return; lastCaptchaUrl = captchaUrl; var newRequest = new PttRequest(captchaUrl) { WrappedRequest = { ContentType = "text/html", Method = "GET" } }; var pttCaptcha = new PttCaptcha() { Url = captchaUrl, RequestUsed = newRequest }; var captchaResolve = ResolveCaptcha(pttCaptcha.RequestUsed, pttCaptcha, 1); pttCaptcha.Value = captchaResolve.Item1; pttCaptcha.FileNameInTempDir = captchaResolve.Item2; var captchaBox = wb.Document.GetElementsByTagName("input")["recaptcha_response_field"]; if (captchaBox == null) return; captchaBox.SetAttribute("value", pttCaptcha.Value); wb.Navigate("javascript:document.forms[0].submit()"); }; Logger.LogProcess("captcha asked for " + request.Url); result = wb.DocumentText; HtmlElement target = wb.Document.GetElementById("recaptcha_widget_div"); if (target != null) { target.AttachEventHandler("onpropertychange", handler2); } /* var pttCaptcha = _captchaRequestResponse.CaptchaImage(null, result); try { var captchaResolve = ResolveCaptcha(pttCaptcha.RequestUsed, pttCaptcha, 1); pttCaptcha.Value = captchaResolve.Item1; pttCaptcha.FileNameInTempDir = captchaResolve.Item2; Logger.LogProcess("captcha value for " + pttCaptcha.FileNameInTempDir+ " is " + pttCaptcha.Value); } catch (Exception exc) { Logger.LogExceptions(exc); Application.ExitThread(); } var captchaBox = wb.Document.GetElementsByTagName("input")["recaptcha_response_field"]; captchaBox.SetAttribute("value", pttCaptcha.Value); Logger.LogProcess("submitting captcha "+ pttCaptcha.Value); wb.Navigate("javascript:document.forms[0].submit()"); Application.ExitThread();*/ } } }; wb.Navigating += (sndr, e) => { Logger.LogProcess("webbrowser navigating to "+ e.Url); if (e.Url.ToString().ToLower().Contains("iswait")) { Logger.LogProcess("redirection done for " + e.Url); redirected = true; } }; Logger.LogProcess("navigating to " +request.Url); wb.Navigate(request.Url); Application.Run(); } }); th.SetApartmentState(ApartmentState.STA); th.Start(); th.Join(); return result; }
private bool GetSinglePttRequest(HtmlNode requestNode, out IPttRequest pttRequest) { pttRequest = null; var url =HttpUtility.HtmlDecode(XmlParse.GetStringNodeValue(requestNode, "url", "")); if (string.IsNullOrEmpty(url)) return false; //request bagimsiz olanlar>bunlardan dolayi bu request biraz daha kapsamli olacak var postValue = HttpUtility.HtmlDecode(XmlParse.GetStringNodeValue(requestNode, "postvalue", "")); var chunked = XmlParse.GetBooleanNodeValue(requestNode, "chunked", false); var condition = HttpUtility.HtmlDecode(XmlParse.GetStringNodeValue(requestNode, "condition", "")); //request ile ilgili olanlar pttRequest = new PttRequest(url, false); pttRequest.RequestType = XmlParse.GetEnumNodeValue<PttRequestType>(requestNode, "type", PttRequestType.None); if (pttRequest.ConditionSatisfied)//satisfy etmeyenleri loglama, webrequest ini doldurma { FillWebRequest(requestNode, pttRequest, url); } pttRequest.Chunked = chunked; pttRequest.PostValue = postValue; pttRequest.Condition = condition; pttRequest.Proxy = _proxy; pttRequest.CookieContainer = _cookieContainer; pttRequest.EventValidationValue = _eventValidationValue; pttRequest.ViewStateValue = _viewStateValue; if(pttRequest.ConditionSatisfied)//satisfy etmeyenleri loglama, webrequest ini doldurma { Logger.LogProcess(pttRequest.ToString()); } return true; }