Ejemplo n.º 1
0
        public string DownloadString(string url, int timeout, string Contains, NetworkCredential credential, int maxlength = 0)
        {
            lock(WriteLock)
            {
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(url);
                    var th = new Thread(() =>
                    {
                        if(timeout > 0)
                            Thread.Sleep(timeout);
                        else
                            Thread.Sleep(13*1000);

                        if(!request.IsNull())
                            request.Abort();
                    });

                    if(timeout > 0)
                    {
                        request.Timeout = timeout;
                        request.ReadWriteTimeout = timeout;
                    }
                    else
                    {
                        request.Timeout = 10*1000;
                        request.ReadWriteTimeout = 10*1000;
                    }

                    request.AllowAutoRedirect = true;
                    request.UserAgent = Consts.SchumixUserAgent;
                    request.Referer = Consts.SchumixReferer;

                    if(!credential.IsNull())
                        request.Credentials = credential;

                    int length = 0;
                    byte[] buf = new byte[1024];
                    var sb = new StringBuilder();

                    using(var response = (HttpWebResponse)request.GetResponse())
                    {
                        using(var stream = response.GetResponseStream())
                        {
                            if(maxlength == 0)
                                maxlength = 10000;

                            while((length = stream.Read(buf, 0, buf.Length)) != 0)
                            {
                                if(sb.ToString().Contains(Contains) || sb.Length >= 10000)
                                    break;

                                buf = Encoding.Convert(Encoding.GetEncoding(response.CharacterSet), Encoding.UTF8, buf);
                                sb.Append(Encoding.UTF8.GetString(buf, 0, length));
                            }
                        }
                    }

                    buf = null;

                    if(!th.IsNull())
                    {
                        th.Interrupt();
                        th = null;
                    }

                    return WebUtility.HtmlDecode(sb.ToString());
                }
                catch(Exception e)
                {
                    Log.Debug("Utilities", sLConsole.GetString("Failure details: {0}"), "(DownloadString) " + e.Message);
                    return string.Empty;
                }
            }
        }