Jeg bruker følgende kode for å søke etter ledige domener. Mine spørsmål er:
* hvis bruker skriver "testdomene.no" vil den søke etter "testdomene.no.no"
hvordan kan jeg luke ut disse?
*Hvordan returnere feilmelding hvis ikke whois-server kan nåes?
Denne er også postet i no.it.programmering.asp
KODE:
<% @Page Language="C#" %>
<% @Import Namespace="System.Net.Sockets" %>
<% @Import Namespace="System.Text" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Collections" %>
<script language="C#" runat="server">
void doQuery(Object sender, EventArgs e)
{
string strDomain;
string strTopLevel = txtTopLevel.SelectedItem.Text;
strDomain=(txtDomain.Text + strTopLevel);
char[] chSplit = {'.'};
string[] arrDomain = strDomain.Split(chSplit);
if (arrDomain.Length != 2)
{
return;
}
int nLength = arrDomain[1].Length;
if (nLength != 2 && nLength != 3)
{
return;
}
Hashtable table = new Hashtable();
table.Add("net", "whois.internic.net");
table.Add("com", "whois.internic.net");
String strServer = "whois.networksolutions.com";
if (table.ContainsKey(arrDomain[1]))
{
strServer = table[arrDomain[1]].ToString();
}
else if (nLength == 2)
{
strServer = "whois.ripe.net";
}
string strResponse;
string strOutput;
bool bSuccess = DoWhoisLookup(strDomain, strServer, out strResponse);
if (bSuccess)
if ((strResponse.IndexOf("% no matches")>0) || (strResponse.IndexOf("No
match")>0) || (strResponse.IndexOf("NOT FOUND")>0) ||
(strResponse.IndexOf("Not found")>0))
{
strOutput = "Domenet <strong>" + strDomain + "</strong> er ledig!";
txtResult.Text = strOutput;
}
else
{
//txtResult.Text = strResponse;
txtResult.Text = "Domenet <strong>" + strDomain + "</strong> er opptatt!";
}
}
bool DoWhoisLookup(String strDomain, String strServer, out String
strResponse)
{
bool bSuccess = false;
strResponse = "Feil!";
TcpClient tcpc = new TcpClient();
try
{
tcpc.Connect(strServer, 43);
}
catch(SocketException ex)
{
strResponse = "Kunne ikke koble til serveren";
return false;
}
strDomain += "\r\n";
Byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain.ToCharArray());
try
{
Stream s = tcpc.GetStream();
s.Write(arrDomain, 0, strDomain.Length);
StreamReader sr = new StreamReader(tcpc.GetStream(), Encoding.ASCII);
StringBuilder strBuilder = new StringBuilder();
string strLine = null;
while (null != (strLine = sr.ReadLine()))
{
strBuilder.Append(strLine+"<br>");
}
tcpc.Close();
bSuccess = true;
strResponse = strBuilder.ToString();
}
catch(Exception e)
{
strResponse = e.ToString();
}
return bSuccess;
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
Skriv inn domenenavnet du vil sjekke:
www. id="txtDomain"
value="ditt-domene" runat="server" />
<asp:DropDownList id=txtTopLevel runat="server">
<asp:ListItem>.no</asp:ListItem>
<asp:ListItem>.com</asp:ListItem>
<asp:ListItem>.net</asp:ListItem>
<asp:ListItem>.org</asp:ListItem>
<asp:ListItem>.biz</asp:ListItem>
</asp:DropDownList>
<asp:Button id="btnQuery" OnClick="doQuery" text="Søk!" runat="server"
/>
<BR>
<asp:label id="txtResult" runat="server" />
</form>
</body>
</html>
-------
Erik Bache