Click here to Skip to main content
15,880,651 members
Articles / Web Development / ASP.NET

Address Book Grabber

Rate me:
Please Sign up or sign in to vote.
1.53/5 (7 votes)
26 Feb 2008CPOL1 min read 61.4K   1.6K   77  
Address Book Grabber
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Login {
    class Program {
        static void Main(string[] args) {
            WebProxy webProxyObj;
            HttpWebRequest httpWebRequestobj;
            string stringObj;
            byte[] data;//to store enoded data
            Stream streamObj;
            HttpWebResponse httpWebResponseObj;
            CookieContainer cookieContainerObj;
            HttpWebRequest homehttpWebRequestobj;
            HttpWebResponse homehttpWebResponseobj;
            Stream homeNames;
            StreamWriter streamWriterObj;
            int getByte;
            String userName, password = null;
            char pwdChar;

            //Proxy setting for the request
            webProxyObj = new WebProxy("ngproxy.Persistent.co.in:8080");
            webProxyObj.UseDefaultCredentials = true;

            cookieContainerObj = new CookieContainer();
            //page request
            httpWebRequestobj = (HttpWebRequest)WebRequest.Create("http://www.youmint.com/web/login.php");
            //setting request header fields
            httpWebRequestobj.Method = "POST";
            httpWebRequestobj.AllowAutoRedirect = false;
            httpWebRequestobj.ContentType = "application/x-www-form-urlencoded; chaset=UTF-8";
            httpWebRequestobj.Proxy = webProxyObj;
            httpWebRequestobj.CookieContainer = new CookieContainer();

            //getting credential from users
            Console.WriteLine("Enter your Login ID:");
            userName = Console.ReadLine();
            Console.WriteLine("Enter your password:");

            //printing * except of character for password
            while ((pwdChar = Console.ReadKey(true).KeyChar) != 13) {
                Console.Write("*");
                password += pwdChar;

            }

            //setting data to be sent
            stringObj = "login_id=" + userName + "&passwd=" + password;
            data = Encoding.UTF8.GetBytes(stringObj);

            httpWebRequestobj.ContentLength = data.Length;

            //getting response from the server
            streamObj = httpWebRequestobj.GetRequestStream();
            streamObj.Write(data, 0, data.Length);
            streamObj.Close();

            //further Requesting by sending cookie with the response
            httpWebResponseObj = (HttpWebResponse)httpWebRequestobj.GetResponse();


            //setting cookie field
            foreach (Cookie cook in httpWebResponseObj.Cookies) {
                cookieContainerObj.Add(cook);
            }

            //getting page by validating the response
            if (httpWebResponseObj.ContentLength == 3) {
                homehttpWebRequestobj = (HttpWebRequest)WebRequest.Create("http://www.youmint.com/web/friends.php?user_id=296804");
                homehttpWebRequestobj.Proxy = webProxyObj;

                //homehttpWebRequestobj.CookieContainer = cookieContainerObj;
                homehttpWebRequestobj.CookieContainer = new CookieContainer();
                homehttpWebRequestobj.CookieContainer.Add(cookieContainerObj.GetCookies(httpWebResponseObj.ResponseUri));

                homehttpWebResponseobj = (HttpWebResponse)homehttpWebRequestobj.GetResponse();
                homeNames = homehttpWebResponseobj.GetResponseStream();

                streamWriterObj = File.CreateText("C:\\WellcomePage.html");

                for (int index = 1; ; index++) {
                    getByte = homeNames.ReadByte();
                    if (getByte == -1)
                        break;
                    else
                        streamWriterObj.Write((char)getByte);
                }

                streamWriterObj.Close();//closing file stream
                homehttpWebResponseobj.Close();//closing response stream
                httpWebResponseObj.Close();
            }
            else {
                //if right credentials are not provided
                Console.WriteLine("Please enter valid user ID and Password");
                httpWebResponseObj.Close();
            }
        }
    }
}

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
Software Developer Persistent System Limited
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions