Ejemplo n.º 1
0
 void OnResponseReceived(WhoisEventArgs e)
 {
     if (ResponseReceived != null)
     {
         ResponseReceived(this, e);
     }
 }
Ejemplo n.º 2
0
        string DoRequest(string server)
        {
            WhoisEventArgs wea = new WhoisEventArgs(
                _url,
                _tld,
                server,
                "Trying to connect to " + server + " (query : " + _domain + ") ...\r\n"
                );

            OnResponseReceived(wea);

            string response = "";

            TcpClient client = null;

            try
            {
                client = new TcpClient(server, 43);
            }
            catch (Exception ex)
            {
                wea = new WhoisEventArgs(
                    _url,
                    _targetServer,
                    _targetServer,
                    "Connection to server " + server + " failed : \r\n" +
                    ex.Message + "\r\n\r\n"
                    );

                OnResponseReceived(wea);
            }

            if (client != null)
            {
                string formatedDomain = _domain + "\r\n";
                byte[] byteUrl        = Encoding.ASCII.GetBytes(formatedDomain);

                Stream s = client.GetStream();

                bool error = true;

                try
                {
                    wea = new WhoisEventArgs(
                        _url,
                        _targetServer,
                        _targetServer,
                        "Connection to server " + _targetServer +
                        " established. Executing query ...\r\n\r\n"
                        );

                    OnResponseReceived(wea);

                    s.Write(byteUrl, 0, formatedDomain.Length);

                    error = false;
                }
                catch (Exception ex)
                {
                    wea = new WhoisEventArgs(
                        _url,
                        _targetServer,
                        _targetServer,
                        "Unable to perform query : \r\n" + ex.Message + "\r\n\r\n"
                        );

                    OnResponseReceived(wea);
                }

                if (!error)
                {
                    StreamReader sr = new StreamReader(s, Encoding.ASCII);

                    response = sr.ReadToEnd() + "\r\n";

                    sr.Close();
                    sr.Dispose();
                    sr = null;

                    wea = new WhoisEventArgs(
                        _url,
                        _targetServer,
                        _targetServer,
                        response
                        );

                    OnResponseReceived(wea);
                }

                s.Close();
                s.Dispose();
                s = null;

                client.Close();
                client = null;
            }

            return(response);
        }