A
Anonieko Ramos
I found this code from ElreyRonald. It is a console but
works by referencing another website.
//--------------------------------------------------------------
// A console application to print IP Address of the current
// workstation (or its router's IP )
//
// Two things are demonstrated here:
// (1) How to get a web page html and
// (2) How to use regular expression to search within a string.
//
// Written by ElreyRonald V. [[email protected]] 2004
//--------------------------------------------------------------
using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;
namespace Network.Utilities
{
class GetIp
{
[STAThread]
static void Main(string[] args)
{
// use a website which displays your IP
string urlString = "http://www.getmyipaddress.com";
// Fetch the html text of the website.
string htmlText = GetPageHtml(urlString);
// Search for the IP within that page.
string regExpr = @".*>(\d+.\d+.\d+.\d+)</*";
Regex rex = new Regex( regExpr, RegexOptions.None);
if (rex.Match( htmlText).Success )
{
string ip = rex.Match( htmlText).Result("$1").Trim();
Console.WriteLine(ip);
}
}
//------------------------------------------------------
// Get the HTML text of a give URL
//------------------------------------------------------
private static String GetPageHtml(string url)
{
StringBuilder sb = new StringBuilder();
try
{
WebRequest myRequest = WebRequest.Create(url);
// Return the response.
WebResponse myHttpWebResponse = myRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream =
myHttpWebResponse.GetResponseStream();
Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with
the required encoding format.
StreamReader readStream = new StreamReader( receiveStream,
encode );
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays
the string to the console.
String str = new String(read, 0, count);
//Console.Write(str);
sb.Append(str);
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
}
catch ( Exception )
{
return "";
}
String strHtml = sb.ToString();
return strHtml.ToString();
}
}
}
works by referencing another website.
//--------------------------------------------------------------
// A console application to print IP Address of the current
// workstation (or its router's IP )
//
// Two things are demonstrated here:
// (1) How to get a web page html and
// (2) How to use regular expression to search within a string.
//
// Written by ElreyRonald V. [[email protected]] 2004
//--------------------------------------------------------------
using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;
namespace Network.Utilities
{
class GetIp
{
[STAThread]
static void Main(string[] args)
{
// use a website which displays your IP
string urlString = "http://www.getmyipaddress.com";
// Fetch the html text of the website.
string htmlText = GetPageHtml(urlString);
// Search for the IP within that page.
string regExpr = @".*>(\d+.\d+.\d+.\d+)</*";
Regex rex = new Regex( regExpr, RegexOptions.None);
if (rex.Match( htmlText).Success )
{
string ip = rex.Match( htmlText).Result("$1").Trim();
Console.WriteLine(ip);
}
}
//------------------------------------------------------
// Get the HTML text of a give URL
//------------------------------------------------------
private static String GetPageHtml(string url)
{
StringBuilder sb = new StringBuilder();
try
{
WebRequest myRequest = WebRequest.Create(url);
// Return the response.
WebResponse myHttpWebResponse = myRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream =
myHttpWebResponse.GetResponseStream();
Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with
the required encoding format.
StreamReader readStream = new StreamReader( receiveStream,
encode );
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string and displays
the string to the console.
String str = new String(read, 0, count);
//Console.Write(str);
sb.Append(str);
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
}
catch ( Exception )
{
return "";
}
String strHtml = sb.ToString();
return strHtml.ToString();
}
}
}