Пример #1
0
        protected string CaptchaPassedResult(IPttRequest request, string htmlSource)
        {
            IPttResponse response = new PttResponse();
            var html = htmlSource;
            var captchaTries = 1;
            while (true)
            {
                var pttCaptcha = _captchaRequestResponse.CaptchaImage(request, html);

                if (_extensiveLoggingNeeded)
                {
                    Logger.LogProcess("RequestResponseWithCaptcha CaptchaPassedResult coming captcha:"+ pttCaptcha);
                }

                try
                {
                    var captchaResolve = ResolveCaptcha(request, pttCaptcha, captchaTries);
                    pttCaptcha.Value = captchaResolve.Item1;
                    pttCaptcha.FileNameInTempDir = captchaResolve.Item2;

                    if (_extensiveLoggingNeeded)
                    {
                        Logger.LogProcess("RequestResponseWithCaptcha CaptchaPassedResult after resolving:" + pttCaptcha);
                    }

                }
                catch (Exception exc)
                {
                    Logger.LogExceptions(exc);
                    return null;
                }
                IPttRequest captchaAddedRequest = _captchaRequestResponse.RequestWithCaptchaValue(request, pttCaptcha);
                if(_extensiveLoggingNeeded)
                {
                    Logger.LogProcess("RequestResponseWithCaptcha CaptchaPassedResult captchaAddedRequest:"+captchaAddedRequest);
                }
                var captchaPassedResult = response.GetResponse(captchaAddedRequest);
                captchaAddedRequest.WrappedRequest = null;
                GC.Collect();

                if (_extensiveLoggingNeeded)
                {
                    Logger.LogProcess("RequestResponseWithCaptcha CaptchaPassedResult captchaPassedResult:" + captchaPassedResult);
                }
                if (_captchaRequestResponse.RepeatFirstRequest)
                {
                    var pttRequestFactory = new PttRequestFactory(request);
                    var firstRequest = pttRequestFactory.Deserialize(string.Format("<Request><Url>{0}</Url><Host>www.tez-tour.com</Host><Referer> <![CDATA[http://www.tez-tour.com]]></Referer></Request>", request.Url));

                    if (_extensiveLoggingNeeded)
                    {
                        Logger.LogProcess("RequestResponseWithCaptcha CaptchaPassedResult RepeatFirstRequest firstrequest:" + firstRequest);
                    }
                    captchaPassedResult = response.GetResponse(firstRequest);
                }

                if (_captchaRequestResponse.CaptchaShown(captchaPassedResult))
                {
                    Logger.LogProcess(string.Format("Incorrect captcha for {0}", pttCaptcha.FileNameInTempDir));
                    html = captchaPassedResult;
                    captchaTries++;
                    continue;
                }
                Logger.LogProcess(string.Format("Correct captcha for {0}", pttCaptcha.FileNameInTempDir));

                return captchaPassedResult;
            }
        }
Пример #2
0
        public string GetResponse(IPttRequest request)
        {
            PrepareForPost(request);

            try
            {
                string result = "";
                using (var response = (HttpWebResponse)request.WrappedRequest.GetResponse())
                {
                    if ((int)response.StatusCode >= 300 && (int)response.StatusCode <= 399)
                    {
                        //redirection varsa request url sini degistirt
                        var uriString = response.Headers["Location"];
                        var pttRequestFactory = new PttRequestFactory(request);
                        var newRequest = pttRequestFactory.Deserialize(string.Format("<Request><Url>{0}</Url><Method>GET</Method><Host>{1}</Host><Referer><![CDATA[{2}]]></Referer></Request>", uriString, request.WrappedRequest.Host, request.WrappedRequest.Referer));
                        request.Response = "Redirect";
                        response.Close();
                        Logger.LogProcess("PttResponse GetResponse url redirect newRequest:" + newRequest);
                        return GetResponse(newRequest);
                    }

                    //http://stackoverflow.com/questions/227575/encoding-problem-with-httpwebresponse
                    var charset = response.CharacterSet;
                    var encoding = Encoding.Default;
                    if (!string.IsNullOrEmpty(charset)) encoding = Encoding.GetEncoding(charset);

                    if (!request.Chunked)
                    {
                        //request.WrappedRequest.CookieContainer.Add(response.Cookies);//buna gerek yok gibi, request.Cookies ile debug edince GetResponse da cookiler otomatik olarak doluyor zaten
                        using (var respStream = response.GetResponseStream())
                        {
                            respStream.ReadTimeout = 1000;
                            using (var sr = new StreamReader(respStream, encoding))
                            {
                                result = sr.ReadToEnd();
                                sr.Close();
                            }
                            response.Close();
                            request.Response = result;
                            return result;
                        }
                    }
                    else
                    {
                        var sb = new StringBuilder();
                        var buf = new byte[8192];

                        using (var resStream = response.GetResponseStream())
                        {
                            int count;
                            do
                            {
                                count = resStream.Read(buf, 0, buf.Length);
                                if (count == 0) continue;

                                var tmpString = encoding.GetString(buf, 0, count);
                                sb.Append(tmpString);
                            } while (count > 0);

                            resStream.Flush();
                            resStream.Close();
                        }

                        response.Close();
                        request.Response = sb.ToString();

                        return sb.ToString();
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {

                request.WrappedRequest.Abort();
                request.WrappedRequest = null;
                GC.Collect();
            }
        }