Accessing remote site


Accessing remote site with CSharp

remote site with c sharp
Image credit: commons.wikimedia.org

Accessing remote site with CSharp is very useful in terms of network programming. This is the best technique to use the predefined abstract classes i.e WebRequest for accessing the website and WebResponse for retrieving the response back.

HTTP content retrieval is an important component for applications these days. Although .NET reduces the need to explicitly retrieve content from the Web through built-in mechanisms in the Web Services framework, ADO.NET and the XML classes, there are still many needs to retrieve Web content directly and manipulate it as text or data downloaded into files.
reference:http://west-wind.com

Example

using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ShowPublicIP
{

class IPLearner
{
/*
STAThread attribute ensures the communication mechanism between the current thr
thread and other threads that may want to talk to it via COM. When you’re
using Windows Forms, depending on the feature you’re using, it may be using COM interop in order to communicate with operating system components.
Good examples of this are the Clipboard and the */

[STAThread]
static void Main()
{
try
{
/* This abstract class requests from remote site*/
WebRequest makeRequest = WebRequest.Create(“http://network-tools.com”);

/* Get the response from the remote site */
using (WebResponse getResponse = makeRequest.GetResponse())
{
using (Stream dataStream = getResponse.GetResponseStream())
using (StreamReader Reader = new StreamReader(dataStream, Encoding.UTF8))
{
string html = Reader.ReadToEnd();
Regex expression = new Regex(@”\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b”);
string AddressString = expression.Match(html).Value;
Console.WriteLine(“My Public IP: ” + AddressString);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.