
2006年9月26日
最近在写个小工具的时候需要通过网址得到域名,不知道怎么Uri里只有得到主机名的方法而没有得到域名的方法,于是只有自己实现一个,但是我感觉这个方法比较变态,希望拿出来和大家一起讨论下,看看有没有更好的方法?
例如:
http://www.google.co.uk,应该返回google.co.uk,
http://www.test.googlepages.com,应该返回googlepages.com
我的程序如下,拿出来讨论下,写的比较弱智,必须预先知道所有的域名后缀。
请大家告诉我好的方法:
private string GetDomain(string url)
{
string host;
Uri uri;
try
{
uri = new Uri(url);
host = uri.Host + " ";
}
catch
{
return "";
}
string[] BeReplacedStrs = new string[] { ".com.cn", ".edu.cn", ".net.cn", ".org.cn", ".co.jp", ".gov.cn", ".co.uk", "ac.cn", ".edu", ".tv", ".info", ".com", ".ac", ".ag", ".am", ".at", ".be", ".biz", ".bz", ".cc", ".cn", ".com", ".de", ".es", ".eu", ".fm", ".gs", ".hk", ".in", ".info", ".io", ".it", ".jp", ".la", ".md", ".ms", ".name", ".net", ".nl", ".nu", ".org", ".pl", ".ru", ".sc", ".se", ".sg", ".sh", ".tc", ".tk", ".tv", ".tw", ".us", ".co", ".uk", ".vc", ".vg", ".ws", ".il", ".li", ".nz" };
foreach (string oneBeReplacedStr in BeReplacedStrs)
{
string BeReplacedStr = oneBeReplacedStr + " ";
if (host.IndexOf(BeReplacedStr) != -1)
{
host = host.Replace(BeReplacedStr, string.Empty);
break;
}
}
int dotIndex = host.LastIndexOf(".");
host = uri.Host.Substring(dotIndex + 1);
return host;
}
谢谢了!
posted @ 2006-09-26 00:56 C# hack 阅读(2047) 评论(16)
编辑

2006年6月7日
private static string ConvertToAbsoluteUrls (string html, Uri relativeLocation) {
IHTMLDocument2 doc = new HTMLDocumentClass ();
doc.write (new object [] { html });
doc.close ();
foreach (IHTMLAnchorElement anchor in doc.links) {
IHTMLElement element = (IHTMLElement)anchor;
string href = (string)element.getAttribute ("href", 2);
if (href != null) {
Uri addr = new Uri (relativeLocation, href);
anchor.href = addr.AbsoluteUri;
}
}
foreach (IHTMLImgElement image in doc.images) {
IHTMLElement element = (IHTMLElement)image;
string src = (string)element.getAttribute ("src", 2);
if (src != null) {
Uri addr = new Uri (relativeLocation, src);
image.src = addr.AbsoluteUri;
}
}
string ret = doc.body.innerHTML;
return ret;
}
posted @ 2006-06-07 00:03 C# hack 阅读(1206) 评论(8)
编辑