这将是相当密集的代码,但只要有 tnid.us 网站,您现在就可以自己做,无需 API:
为什么不使用电话号码的 URL 在隐藏的浏览器窗口中打开 IE?看起来 URL 将采用 http://tnid.us/search.php?q=########## 的格式,其中 # 代表一个数字。所以你需要一个文本框、一个标签和一个按钮。我将文本框称为“txtPhoneNumber”,将标签称为“lblCarrier”,按钮将调用我在“OnClick”下方的函数。
按钮函数使用 MSHtml.dll 和 SHDocVW.dll 创建 IE 实例,并对浏览器“对象”中的 HTML 进行页面抓取。然后你把它解析下来。您必须首先安装 Visual Studio 2005 附带的互操作性程序集 (C:\Program Files\Common Files\Merge Modules\vs_piaredist.exe)。那么:
1> 在 Visual Studio.NET 中创建一个新的 Web 项目。
2> 添加对 SHDocVw.dll 和 Microsoft.mshtml 的引用。
3> 在 default.aspx.cs 中,在顶部添加这些行:
using mshtml;
using SHDocVw;
using System.Threading;
4> 增加如下功能:
protected void executeMSIE(Object sender, EventArgs e)
{
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();
object o = System.Reflection.Missing.Value;
TextBox txtPhoneNumber = (TextBox)this.Page.FindControl("txtPhoneNumber");
object url = "http://tnid.us/search.php?q=" + txtPhoneNumber.Text);
StringBuilder sb = new StringBuilder();
if (ie != null) {
ie.Navigate2(ref url,ref o,ref o,ref o,ref o);
ie.Visible = false;
while(ie.Busy){Thread.Sleep(2);}
IHTMLDocument2 d = (IHTMLDocument2) ie.Document;
if (d != null) {
IHTMLElementCollection all = d.all;
string ourText = String.Empty;
foreach (object el in all)
{
//find the text by checking each (string)el.Text
if ((string)el.ToString().Contains("Current Phone Company"))
ourText = (string)el.ToString();
}
// or maybe do something like this instead of the loop above...
// HTMLInputElement searchText = (HTMLInputElement)d.all.item("p", 0);
int idx = 0;
// and do a foreach on searchText to find the right "<p>"...
foreach (string s in searchText) {
if (s.Contains("Current Phone Company") || s.Contains("Original Phone Company")) {
idx = s.IndexOf("<strong>") + 8;
ourText = s.Substring(idx);
idx = ourText.IndexOf('<');
ourText = ourText.Substring(0, idx);
}
}
// ... then decode "ourText"
string[] ourArray = ourText.Split(';');
foreach (string s in ourArray) {
char c = (char)s.Split('#')[1];
sb.Append(c.ToString());
}
// sb.ToString() is now your phone company carrier....
}
}
if (sb != null)
lblCarrier.Text = sb.ToString();
else
lblCarrier.Text = "No MSIE?";
}
由于某种原因,当我直接使用 tnid.us 网站时,我没有得到“当前电话公司”,但只有原始网站。所以你可能想让代码测试它得到了什么,即
bool currentCompanyFound = false;
if (s.Contains("Current Telephone Company")) { currentCompanyFound = true }
我让它检查上面的任何一个,所以你会得到一些东西。代码应该做的是找到
之间的 HTML 区域
<p class="lt">Current Telephone Company:<br /><strong>
和
</strong></p>
我让它寻找索引
<strong>
并添加该单词的字符以到达起始位置。我不记得您是否可以对 .indexOf 使用字符串或仅使用字符。但是你明白了,你或其他人可能会找到一种方法让它从那里开始工作。
您返回的文本是用字符代码编码的,因此您必须对其进行转换。我在上面给了你一些代码,应该可以帮助...