Мысли вслух…

Определение ТИЦ C#

Автор: Июн.30.2009 Категория: Программирование

Сегодня на ачате чувак попросил перевести с php на C#

Ниже код на php:


function getCI($url)
{
$url = str_replace("www.", "", $url);
$ci_url = "http://bar-navig.yandex.ru/u?ver=2&show=32&url=http://www.".$url."/";
$ci_data = implode("", file("$ci_url"));
preg_match("/value=\"(.\d*)\"/", $ci_data, $ci);
if ($ci[1] == "")
return 0; // Если не смогли определить ТИЦ...
else
return $ci[1]; // Вот оно счастье...
}

Вот что из этого вышло:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

public class Test
{
    public static WebResponse GetResponse(string url, bool redirect)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                request.Method = "GET";
                request.AllowAutoRedirect = redirect;
                return request.GetResponse();
            }
            catch
            {
                return null;
            }
        }

        public static string GetPageSrc(string url)
        {
            var response = GetResponse(url, true);
            if (response == null)
            {
                return "";
            }
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1251)))
            {
                return reader.ReadToEnd();
            }
        }

    private static int getCI(string url)
    {
        var pageScr = GetPageSrc(string.Format("http://bar-navig.yandex.ru/u?ver=2&show=32&url=http://www.{0}/", url));
        var reg = new Regex("value=\"(.\\d*)\"", RegexOptions.IgnoreCase);
        return reg.IsMatch(pageScr) ? int.Parse(reg.Match(pageScr).Groups[1].Value) : 0;
    }

    public static void Main()
    {
        Console.WriteLine(getCI("gw.kz"));
        Console.ReadLine();
    }
}
:

2 Comments for this entry

Leave a Reply

:bad: :beer: :biggrin: :blink: :blush: :bomb: :confused: :cool: :crazy: :cry: :dont_know: :eek: :evil: :dance: :heart: :idea: :joke: :kiss: :lol: :mad: :music: :rose: :sad: :smile: :surprised: :tongue: :yahoo: :wall: :wink:
 

Поиск