locked
How to read a URL Contents from ASP.NET RRS feed

  • Question

  • User-169337105 posted

    In my last project I was required to read a URL Contents and look for certain tags. Just sharing a part of it

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Configuration;
    using log4net;

    public static class URLReader
    {
    #region Constants
    private const int TIMEOUT = 30;
    #endregion

    #region
    Public Methods
    /// <summary>
    /// Reads contents of a URL
    /// </summary>
    /// <param name="url"></param>

    public static String LoadInformation(String url)
    {
    HttpWebRequest myWebRequest = null;
    HttpWebResponse myWebResponse = null;
    Stream receiveStream = null;
    Encoding encode = null;
    StreamReader readStream = null;
    string text = null;

    try
    {
    myWebRequest = HttpWebRequest.Create(url) as HttpWebRequest;

    myWebRequest.Timeout = TIMEOUT;
    myWebRequest.ReadWriteTimeout = TIMEOUT;

    myWebResponse = myWebRequest.GetResponse() as HttpWebResponse;
    receiveStream = myWebResponse.GetResponseStream();
    encode = System.Text.Encoding.GetEncoding("utf-8");
    readStream = new StreamReader(receiveStream, encode);
    text = readStream.ReadToEnd().ToLower();
    if (readStream != null) readStream.Close();
    if (receiveStream != null) receiveStream.Close();
    if (myWebResponse != null) myWebResponse.Close();
    }
    catch (Exception)
    {
    //Do Something
    }
    finally
    {
    readStream = null;
    receiveStream = null;
    myWebResponse = null;
    myWebRequest = null;
    }
    return text;
    }
    #endregion
    }


     
    Monday, December 8, 2008 4:32 AM

All replies

  • User-2005691517 posted

    Since you have posted this just for information, mark it as answered.

    Saturday, July 16, 2011 11:38 PM