Build a Smartphone Reverse Phone Number Lookup App
by Peter A. Bromberg, Ph.D.

Peter Bromberg

Have you ever gotten a call on your Smartphone from somebody, and you either missed the call or didn't answer it? And then you look in your Call History and you see this number that you don't recognize. And your phone didn't map it to a contact either. So who was it? Now, you can decide whether you want to call them back, by running this cool little webscraper Smartphone "Reverse Phone Number Lookup App":

Reerse Phone

This is one of those very easy-to-implement "web scraping" exercises, there must be a dozen reverse phone number lookup services online. All I had to do is pick one, set up an HttpWebRequest with the correct Url, and do some very simple string manipulation to scrape out only what I needed. I'm not going to talk about ethics here, screenscraping is certainly bad if you steal from somebody's web site and attempt to profit from it. Fact of the matter is, if I tried to use their page with Pocket IE in my Smartphone, they have so much "glop" on there it would clearly be unusable. So scrape away I did!



Now, first you may wish to try out this IFRAME to a script-only page that has exactly the same code and try it out:


Now here's the code for the Form, which is the only class in the application:

#region Using directives
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Net ;
using System.IO;
#endregion
namespace ReversePhone
{
    public class Form1 : System.Windows.Forms.Form
    {
        private TextBox textBox1;
        private MenuItem menuItem1;
        private MenuItem menuItem2;
        private Label label1;
        private System.Windows.Forms.MainMenu mainMenu1;

        public Form1()
        {
            InitializeComponent();
        }       
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.menuItem1);
            this.mainMenu1.MenuItems.Add(this.menuItem2);
            // 
            // menuItem1
            // 
            this.menuItem1.Text = "Lookup";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Text = "Quit";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(4, 4);
            this.textBox1.Size = new System.Drawing.Size(152, 24);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(0, 37);
            this.label1.Size = new System.Drawing.Size(176, 143);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(176, 180);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Menu = this.mainMenu1;
            this.Text = "Reverse Phone Lookup";

        }
        #endregion

        static void Main()
        {
            Application.Run(new Form1());
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
         string url="http://www.whitepages.com/10001/search/Reverse_Phone?phone=";
   url+=this.textBox1.Text;
   HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
   req.Method = "GET";
   Stream stm ;
   try
   {
    WebResponse resp = req.GetResponse();
    stm = resp.GetResponseStream();
    StreamReader r = new StreamReader(stm);
    string resultStr=r.ReadToEnd();     
    resultStr=resultStr.Substring(resultStr.IndexOf("listing?")+8); 
    resultStr = resultStr.Substring(0,resultStr.IndexOf(">E-mail")-1 ) ;
    resultStr=resultStr.Replace("&amp;","|") ;
    string[] stuff =resultStr.Split('|');
    string finalStr=String.Empty ;
    foreach(string s in stuff)
    {
     string[] pair=s.Split('=');
     finalStr+=pair[1]+" ";
    }
    finalStr=finalStr.Replace("%20"," ") ;
    this.label1.Text=finalStr;
   }
   catch
   {
             // well, we either got it or we didn't... 
             this.label1.Text="Not Found";
   }
  }   

    }
}

You will notice I didn't even bother figuring out fancy Regex patterns above. All i did was "chop off" the interesting part of the web page and massage what was left with some String Splits to get a displayable result for my Label. Bear in mind, if they change the page (which sites occasionally do) both Regex and string manipulation are likely to get broken. The downloadable Solution zip has a completely built CAB deployment file you can copy directly to your SmartPhone via ActiveSync and use immediately. By the way- you can rest assured that the call results shown in the example above were a "wrong number" for sure.

Download the Visual Studio.NET 2005 Smart Device solution that accompanies this article

 


Peter Bromberg is a C# MVP, MCP, and .NET consultant who has worked in the banking and financial industry for 20 years. He has architected and developed web - based corporate distributed application solutions since 1995, and focuses exclusively on the .NET Platform.
Article Discussion: