Onur

Özten

Bilgisayar Mühendisi & Yazılım Uzmanı


ASP.Net $file_get_contents

protected void Page_Load(object sender, EventArgs e)
{
    var site = file_get_contents("http://www.xxxxxx.com/");
    var title = GetTitle(site);
}

protected string file_get_contents(string fileName)
{
    string sContents = string.Empty;
    if (fileName.ToLower().IndexOf("http:") > -1)
    { 
        // URL 
        System.Net.WebClient wc = new System.Net.WebClient();
        byte[] response = wc.DownloadData(fileName);
        sContents = System.Text.Encoding.UTF8.GetString(response);
    }
    else
    {
        // Regular Filename 
        System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
        sContents = sr.ReadToEnd();
        sr.Close();
    }
    return sContents;
}

static string GetTitle(string file)
{
    Match m = Regex.Match(file, @"\s*(.+?)\s*");
    if (m.Success)
    {
        return m.Groups[1].Value;
    }
    else
    {
        return "";
    }
}