Exemplo n.º 1
0
        public static HtmlDocument GetHtmlDocument(string url)
        {
            HtmlDocument doc = null;

            int  retries = 3;
            bool downOk  = false;

            while (true)
            {
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(url);
                    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
                    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    string          xml;
                    using (XmlSanitizingStream reader =
                               new XmlSanitizingStream(response.GetResponseStream()))
                    {
                        xml = reader.ReadToEnd();

                        Uri uri = new Uri(url);
                        xml = xml.Replace(" src=\"/", " src=\"http://" + uri.Host + "/");
                    }

                    // xml contains no illegal characters
                    doc = new HtmlDocument
                    {
                        OptionOutputAsXml   = true,
                        OptionFixNestedTags = true
                    };
                    doc.LoadHtml(xml);

                    downOk = true;

                    break;
                }
                catch (Exception ex)
                {
                    Logging?.Log("GetHtmlDocument ERROR", ex.Message);
                    if (--retries == 0)
                    {
                        break;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
            }
            if (downOk)
            {
                return(doc);
            }
            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Supports RSS 1, 2 and ATOM 1.0 feed standards
        /// </summary>
        /// <param name="url"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        internal SyndicationFeed GetFeed(Uri url, int timeout, NetworkCredential credentials)
        {
            SyndicationFeed feed    = null;
            HttpWebRequest  request = (HttpWebRequest)WebRequest.Create(url);

            request.UserAgent = Configuration.UserAgent;
            request.Timeout   = timeout;
            if (credentials != null)
            {
                request.Credentials     = credentials;
                request.PreAuthenticate = true;
            }

            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    XmlSanitizingStream stream = new XmlSanitizingStream(response.GetResponseStream());
                    var xml = stream.ReadToEnd();
                    using (RssXmlReader reader = new RssXmlReader(Flush(xml)))
                    {
                        if (Atom03FeedFormatter.CanReadFrom(reader))
                        {
                            var aff = new Atom03FeedFormatter();
                            aff.ReadFrom(reader);
                            feed = aff.Feed;
                        }
                        else if (Rss10FeedFormatter.CanReadFrom(reader))
                        {
                            // RSS 1.0
                            var rff = new Rss10FeedFormatter();
                            rff.ReadFrom(reader);
                            feed = rff.Feed;
                        }
                        else
                        {
                            // RSS 2.0 or Atom 1.0
                            try
                            {
                                feed = CustomSyndicationFeed.Load(reader);
                            }
                            catch (XmlException ex)
                            {
                                logger.Error(ex);
                                throw;
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                logger.Error(ex);
            }
            catch (SocketException ex)
            {
                logger.Error(ex);
            }
            catch (IOException ex)
            {
                logger.Error(ex);
            }
            return(feed);
        }
Exemplo n.º 3
0
        private Tuple <FeedReadResult, string> RetrieveFeed()
        {
            try
            {
                // Add extra security protocols
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                // Create the web request
                var request = WebRequest.Create(new Uri(Source));

                // If this is an http request set some special properties
                if (request is HttpWebRequest webRequest)
                {
                    // Make sure to use HTTP version 1.1
                    webRequest.ProtocolVersion = HttpVersion.Version11;

                    // Set that we'll accept compressed data
                    webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                    // Set a timeout
                    webRequest.Timeout = 10000;

                    // Make sure the service point closes the connection right away
                    webRequest.ServicePoint.ConnectionLeaseTimeout = 0;

                    // If we need to authenticate then set the credentials
                    if (Authenticate)
                    {
                        webRequest.Credentials = new NetworkCredential(Username, Password, Domain);
                    }

                    // Set a user agent string
                    if (string.IsNullOrWhiteSpace(Settings.Default.DefaultUserAgent))
                    {
                        webRequest.UserAgent = "FeedCenter/" + UpdateCheck.LocalVersion;
                    }
                    else
                    {
                        webRequest.UserAgent = Settings.Default.DefaultUserAgent;
                    }
                }

                // Set the default encoding
                var encoding = Encoding.UTF8;

                // Attempt to get the response
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    // If the response included an encoding then change the encoding
                    if (response.ContentEncoding.Length > 0)
                    {
                        encoding = Encoding.GetEncoding(response.ContentEncoding);
                    }

                    // Get the response stream
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            return(Tuple.Create(FeedReadResult.NoResponse, string.Empty));
                        }

                        // Create the text reader
                        using (StreamReader textReader = new XmlSanitizingStream(responseStream, encoding))
                        {
                            // Get the feed text
                            var feedText = textReader.ReadToEnd();

                            // Get rid of any leading and trailing whitespace
                            feedText = feedText.Trim();

                            // Clean up common invalid XML characters
                            feedText = feedText.Replace("&nbsp;", "&#160;");

                            // Find ampersands that aren't properly escaped and replace them with escaped versions
                            var r = new Regex("&(?!(?:[a-z]+|#[0-9]+|#x[0-9a-f]+);)");
                            feedText = r.Replace(feedText, "&amp;");

                            return(Tuple.Create(FeedReadResult.Success, feedText));
                        }
                    }
                }
            }
            catch (IOException ioException)
            {
                Tracer.WriteLine(ioException.Message);

                return(Tuple.Create(FeedReadResult.ConnectionFailed, string.Empty));
            }
            catch (WebException webException)
            {
                var result = FeedReadResult.UnknownError;

                if (webException.Response is HttpWebResponse errorResponse)
                {
                    switch (errorResponse.StatusCode)
                    {
                    case HttpStatusCode.InternalServerError:

                        return(Tuple.Create(FeedReadResult.ServerError, string.Empty));

                    case HttpStatusCode.NotModified:

                        return(Tuple.Create(FeedReadResult.NotModified, string.Empty));

                    case HttpStatusCode.NotFound:

                        return(Tuple.Create(FeedReadResult.NotFound, string.Empty));

                    case HttpStatusCode.Unauthorized:
                    case HttpStatusCode.Forbidden:

                        return(Tuple.Create(FeedReadResult.Unauthorized, string.Empty));
                    }
                }

                switch (webException.Status)
                {
                case WebExceptionStatus.ConnectFailure:
                case WebExceptionStatus.NameResolutionFailure:
                    result = FeedReadResult.ConnectionFailed;

                    break;

                case WebExceptionStatus.Timeout:
                    result = FeedReadResult.Timeout;

                    break;
                }

                Tracer.WriteException(webException);

                if (result == FeedReadResult.UnknownError)
                {
                    Debug.Print("Unknown error");
                }

                return(Tuple.Create(result, string.Empty));
            }
            catch (Exception exception)
            {
                Tracer.WriteLine(exception.Message);

                return(Tuple.Create(FeedReadResult.UnknownError, string.Empty));
            }
        }
Exemplo n.º 4
0
        public static List <FileCargaBean> ejecutarArchivoKML(String fileLocation)
        {
            List <FileCargaBean> lista = new List <FileCargaBean>();

            List <String> arrArchivosCargados = new List <String>();

            String[] extensions;
            //if (ConfigurationManager.AppSettings["DTSX"].Trim() == "1")
            extensions = new String[] { "*.kml" };
            //else
            //    extensions = new String[] { "*.xls" };

            foreach (String extension in extensions)
            {
                String[] filesArr = Directory.GetFiles(fileLocation, extension, SearchOption.TopDirectoryOnly);
                foreach (String file in filesArr)
                {
                    arrArchivosCargados.Add(file);
                }
            }

            //List<FileCargaBean> listaArchivos = new List<FileCargaBean>();
            FileCargaBean FileBean;

            String xml;

            foreach (String file in arrArchivosCargados)
            {
                FileBean = new FileCargaBean();
                try
                {
                    using (XmlSanitizingStream reader = new XmlSanitizingStream(new FileStream(file, FileMode.Open)))
                    {
                        xml = reader.ReadToEnd();
                    }

                    XmlDocument xmlDocument = new XmlDocument();

                    //xmlDocument.Load(file);
                    xmlDocument.LoadXml(xml);
                    xmlDocument.RemoveChild(xmlDocument.FirstChild);

                    int           num1            = 0;
                    int           num2            = 0;
                    int           num3            = 0;
                    int           num4            = 0;
                    XmlElement    documentElement = xmlDocument.DocumentElement;
                    StringBuilder stringBuilder   = new StringBuilder("<lp>");
                    foreach (XmlElement xmlElement in documentElement.FirstChild.ChildNodes)
                    {
                        if ("PLACEMARK".Equals(xmlElement.Name.ToUpper()))
                        {
                            ++num1;

                            string str = GeocercaController.registrarGeocercaAPartirDeXML(xmlElement.OuterXml);
                            if (str.Contains("|"))
                            {
                                string[] strArray = str.Split('|');
                                int      result1  = 0;
                                int.TryParse(strArray[0].ToString(), out result1);
                                num3 += result1;
                                int result2 = 0;
                                int.TryParse(strArray[1].ToString(), out result2);
                                num4 += result2;
                                ++num2;
                            }
                        }
                    }
                    FileBean.archivo      = file.Substring(file.LastIndexOf("\\") + 1, file.LastIndexOf(".") - file.LastIndexOf("\\") - 1);
                    FileBean.total        = num2;//num2;
                    FileBean.subidos      = num2;
                    FileBean.insertados   = num3;
                    FileBean.actualizados = num4;
                }
                catch (Exception e)
                {
                    FileBean.errorExecute += e.Message;
                }
                lista.Add(FileBean);
            }

            deleteDataFiles(fileLocation);

            return(lista);
        }