@byxiaoxie5 年前

03/12
23:41
Home

C# 查IP国家/地区

引用:using System.Net;
/// <summary>
/// 查IP国家/地区
/// </summary>
/// <param name="strIP"></param>
/// <returns></returns>
public static string GetIPCitys(string strIP)
{
    string Url = null;
    string Text = null;
    string c = null;
    try
    {
        Url = "http://opendata.baidu.com/api.php?query=" + strIP + "&co=&resource_id=6006&t=1412300361645&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery1102026811896078288555_1412299994977&_=1412299994981";
	WebRequest wReq = System.Net.WebRequest.Create(Url);
        wReq.Timeout = 2000;
        WebResponse wResp = wReq.GetResponse();
        Stream respStream = wResp.GetResponseStream();
        StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("gb2312"));
        Text = reader.ReadToEnd();
		
	//取文本内容中间文字 - 开始
        int i = Text.IndexOf("location\":\"") + 11;
        int j = Text.IndexOf("titlecont")-5;
        c = Text.Substring(i, j - i + 2);
	//取文本内容中间文字 - 结束
		
        if (c != "")
        {
            return c;
        }
        else
        {
            c = "未知";
        }
    }
    catch
    {
        c = "错误";
    }
    return c;
}

 

C# 查IP国家/地区

加载中……