All pastes #18132 Raw Edit

url annotation

public text v1 · immutable
#18132 ·published 2005-07-20 14:21 UTC
rendered paste body
class HttpHead
    {
        TcpClient webTC = new TcpClient();
        NetworkStream WebStream;
        StreamReader WebRead;
        StreamWriter WebWrite;
        static int size;

        public void annotateURL(string url)
        {
            webConnect(url);
            cleanConnections();
        }

        private void webConnect(string url)
        {
            Console.WriteLine("WEbConnect");

            string inputLine;
            try
            {
                if (!webTC.Connected)
                    webTC.Connect(url.Split('/')[2], 80);
                WebStream = webTC.GetStream();
                WebRead = new StreamReader(WebStream);
                WebWrite = new StreamWriter(WebStream);


                //Send HEAD
                if (url.Split('/').Length >= 4)
                    WebWrite.WriteLine("HEAD " + url.Substring(7 + url.Split('/')[2].Length) + " HTTP/1.0");
                else
                    WebWrite.WriteLine("HEAD / HTTP/1.0");

                WebWrite.Flush();
                WebWrite.WriteLine("");
                WebWrite.Flush();
                inputLine = WebRead.ReadLine();
                bool annotated = false;
                while (true)
                {
                    do
                    {
                        if (!annotated)
                            annotated = parseTitle(inputLine, url);
                        if (annotated)
                            break;
                    } while ((inputLine = WebRead.ReadLine()) != null);
                    break;
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }

        private bool parseTitle(string inputLine, string url)

        {
            int acceptableLength = 0;
            WebClient web = new WebClient();
            Console.WriteLine(inputLine);
            if (inputLine.Contains("Content-Length:"))
            {
                size = Int32.Parse((inputLine.Split(' ')[1]));
                if (size <= 1073741824)
                    acceptableLength = 1;
                else
                    acceptableLength = 2;

            }

            if (inputLine.Contains("Content-Type:") && inputLine.Contains("text/html") && (acceptableLength == 2 || acceptableLength == 0))
            {
                byte[] httpByteStream = web.DownloadData(url);

                string httpString = Encoding.Default.GetString(httpByteStream);
                string[] titleSplitArray = Regex.Split(httpString, "<title [^>]*>|<TITLE[^>]>|<title>|<TITLE>");
                string[] titleContent = Regex.Split(titleSplitArray[1], "</title|</TITLE");

                if (url.Length >= 70)
                {
                    byte[] responseArray = web.DownloadData("http://tinyurl.com/api-create.php?url=" + url);
                    IrcLib.say("[" + titleContent[0].Replace('\n', ' ') + "] " + Encoding.ASCII.GetString(responseArray));
                    IrcLib.say("[" + titleContent[0].Replace('\n', ' ') + "] " + Encoding.ASCII.GetString(responseArray));
                    WebRead.Close();
                    WebWrite.Close();
                    return true;
                }
                else
                {
                    IrcLib.say("[" + titleContent[0].Replace('\n', ' ') + "]");
                    WebRead.Close();
                    WebWrite.Close();
                    return true;
                }
            }
            else if (inputLine.Contains("Content-Type:") && !inputLine.Split(' ')[1].Contains("text/html"))
            {
                Console.WriteLine("[" + inputLine.Split(' ')[1] + ": " + size + "]");
                IrcLib.say("[" + inputLine.Split(' ')[1] + ": " + size + "]");
                WebRead.Close();
                WebWrite.Close();
                return true;
            }
            return false;
        }
        private void cleanConnections()
        {
            WebWrite.Close();
            WebRead.Close();
            WebStream.Close();
            webTC.Close();


        }

    }