Source/NFX/IOMiscUtils.cs
/// <summary>
/// Fetch the content of a given URL.
/// </summary>
/// <returns>
/// Return fetched URL as a string, or null string when resulting status code is not HttpStatusCode.OK.
/// </returns>
Fetch the content of a given URL.
Fetch the content of a given URL .
public static KeyValuePair<HttpStatusCode,string> GetURL(string urlAddress, int timeout = 5000)
        {
            var req     = (HttpWebRequest)WebRequest.Create(urlAddress);
            req.Timeout = timeout;
            var res     = (HttpWebResponse)req.GetResponse();
            try
            {
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    var strm = res.GetResponseStream();
                    var rds  = res.CharacterSet == null
                             ? new StreamReader(strm)
                             : new StreamReader(strm, Encoding.GetEncoding(res.CharacterSet));
                    var s = rds.ReadToEnd();
                    rds.Close();
                    return new KeyValuePair<HttpStatusCode, string>(res.StatusCode, s);
                }
            }
            finally
            {
                res.Close();
            }
            return new KeyValuePair<HttpStatusCode, string>(res.StatusCode, null);
        }
public static KeyValuePair < HttpStatusCode , string > GetURL ( string urlAddress , int timeout = 5000 ) { var req = ( HttpWebRequest ) WebRequest . Create ( urlAddress ) ; req . Timeout = timeout ; var res = ( HttpWebResponse ) req . GetResponse ( ) ; try { if ( res . StatusCode == HttpStatusCode . OK ) { var strm = res . GetResponseStream ( ) ; var rds = res . CharacterSet == null ? new StreamReader ( strm ) : new StreamReader ( strm , Encoding . GetEncoding ( res . CharacterSet ) ) ; var s = rds . ReadToEnd ( ) ; rds . Close ( ) ; return new KeyValuePair < HttpStatusCode , string > ( res . StatusCode , s ) ; } } finally { res . Close ( ) ; } return new KeyValuePair < HttpStatusCode , string > ( res . StatusCode , null ) ; }
Fetch the content of a given URL.
Fetch the content of a given URL .
Return fetched URL as a string, or null string when resulting status code is not HttpStatusCode.OK.
Return fetched URL as a string or null string when resulting status code is not HttpStatusCode . OK .
urlAddress
string
timeout
int