Click here to Skip to main content
15,894,646 members
Articles / Programming Languages / C#

Website Investigator

Rate me:
Please Sign up or sign in to vote.
4.36/5 (27 votes)
5 Aug 2009CPOL5 min read 47.6K   1.1K   50  
Gets website registration information
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;

namespace QIP_Country_Match
{
    class UpdateDatabase
    {
        string gzFileName;
        System.Net.WebClient w;
        System.Uri url1;

        public UpdateDatabase()
        {
            gzFileName = Environment.GetEnvironmentVariable("TEMP") + "\\IpToCountry.gz";
            w = new System.Net.WebClient();
            url1 = new Uri(@"http://www.software77.net/cgi-bin/ip-country/geo-ip.pl?action=download");
        }

        public void Start()
        {
            ConnectAndDownload();
            UncompressAndExit();
        }

        private void ConnectAndDownload()
        {

       //start downloading
        ConnectAndDownload:
            {
                try
                {
                    w.DownloadFile(url1, gzFileName);
                }
                catch (System.Net.WebException)
                {
                    DialogResult dr = MessageBox.Show("Cannot connect to server!\nYou may not be connected to the internet or the server might be busy at this moment\nYou may need to try later\n\nNote: Our online database is locked everyday for new updates at 12 UTC, so please\nDO NOT attempt to update during this time!", "Error", MessageBoxButtons.RetryCancel);
                    if (dr == DialogResult.Retry)
                        goto ConnectAndDownload;
                    else
                        Application.Exit();
                }
            }
        }

        private void UncompressAndExit()
        {
            UncompressFile();
            System.IO.File.Delete(gzFileName);
        }

        private void UncompressFile()
        {
            try
            {
                FileStream sourceFile = File.OpenRead(gzFileName);
                FileStream destinationFile = File.Create(gzFileName.TrimEnd(".gz".ToCharArray()) + ".csv");

                byte[] buffer = new byte[sourceFile.Length * 100];
                int n;

                using (GZipStream input = new GZipStream(sourceFile,
                    CompressionMode.Decompress, false))
                {


                    n = input.Read(buffer, 0, buffer.Length);
                    destinationFile.Write(buffer, 0, n);
                }

                // Close the files.
                sourceFile.Close();
                destinationFile.Close();
            }
            catch (Exception) { }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired QSoft
Yemen Yemen
Biography?! I'm not dead yet!
www.QSoftOnline.com

Comments and Discussions