@Seymour
2018-08-08T17:02:06.000000Z
字数 1961
阅读 1066
未分类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Http;
using Winista.Text.HtmlParser.Visitors;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Nodes;
namespace MSNPSharpClient.SearchIP
{
class SearchIP
{
public string GetSearchIP(string IP)
{
GetSearchIPHtmlCode getUrlAndHtmlCode = new GetSearchIPHtmlCode();
string str = "";
Lexer lexer = new Lexer(getUrlAndHtmlCode.getSearchIPHtmlCode(IP));
Parser myparser = new Parser(lexer);
NodeFilter filter = new NodeClassFilter(typeof(TableTag));
NodeList nodelist = myparser.ExtractAllNodesThatMatch(filter);
str = nodelist.ElementAt(2).ToPlainTextString();
string strInfo = "";
Char[] c = str.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i].ToString() != "/n" && c[i].ToString() != "/t" && c[i].ToString() != "/r")
{
strInfo += c[i].ToString().Trim();
}
}
int m = nodelist.Count;
strInfo = strInfo.Replace(" ", "");
strInfo = strInfo.Replace("如果您发现查询结果不详细或不正确,请使用IP数据库自助添加功能进行修正", "");
//ip138.com IP查询(搜索IP地址的地理位置)
strInfo = strInfo.Replace("ip138.comIP查询(搜索IP地址的地理位置)", "");
//Console.WriteLine(strInfo);
//Console.ReadKey();
return strInfo;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace MSNPSharpClient.SearchIP
{
class GetSearchIPHtmlCode
{
public string getSearchIPUrl(string IP)
{
Encoding gb2312 = Encoding.GetEncoding("gb2312");
//string SimplifiedChinese=Strings.StrConv(City);//繁體轉換成簡體失敗
string strIP = System.Web.HttpUtility.UrlEncode(IP, gb2312);
//http://www.ip138.com/ips.asp?ip=25.205.25.23
string IDSearchUrl = "http://www.ip138.com/ips.asp?ip=" + strIP;
//Console.WriteLine(CityUrl);
//Console.ReadKey();
return IDSearchUrl;
}
public string getSearchIPHtmlCode(string IP)
{
string url = getSearchIPUrl(IP);
WebClient webclient = new WebClient();
webclient.Encoding = Encoding.Default;
string htmlcode = webclient.DownloadString(url);
return htmlcode;
}
}
}