Created
January 14, 2016 03:44
-
-
Save erickgirard/405a78aa29940e7d512a to your computer and use it in GitHub Desktop.
whois client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.AspNet.Mvc; | |
namespace WebApplication1.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
private const int WhoisServerDefaultPortNumber = 43; | |
private const string DomainRecordType = "domain"; | |
private const string DotComWhoisServer = "whois.verisign-grs.com"; | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
public string Look(string id) | |
{ | |
return Lookup(id); | |
} | |
private static string Lookup(string domainName) | |
{ | |
using (TcpClient whoisClient = new TcpClient()) | |
{ | |
whoisClient.Connect(DotComWhoisServer, WhoisServerDefaultPortNumber); | |
string domainQuery = DomainRecordType + " " + domainName + "\r\n"; | |
byte[] domainQueryBytes = Encoding.ASCII.GetBytes(domainQuery.ToCharArray()); | |
Stream whoisStream = whoisClient.GetStream(); | |
whoisStream.Write(domainQueryBytes, 0, domainQueryBytes.Length); | |
StreamReader whoisStreamReader = new StreamReader(whoisClient.GetStream(), Encoding.ASCII); | |
string streamOutputContent = ""; | |
List<string> whoisData = new List<string>(); | |
while (null != (streamOutputContent = whoisStreamReader.ReadLine())) | |
{ | |
whoisData.Add(streamOutputContent); | |
} | |
//whoisClient.Close(); | |
return String.Join(Environment.NewLine, whoisData); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment