C#根据淘宝接口网址获取客户端访问IP和网络运营商

网络运营商会为每台联网的电脑分配公网IP,如何获取它们??

话不多说直接上代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using GW.ThirdOpenAPI.AdminWeb.Infrastructure; namespace GW.ThirdOpenAPI.AdminWeb.Models.Common
{
public class GetOutIpHelper
{
public static GetOutIpHelper Instance { get; } = new GetOutIpHelper(); private string url = "http://ip.taobao.com/service/getIpInfo.php?ip=myip";
public TaobaoData GetIPData()
{
string areaJson;
TaobaoData theData;
try
{
//创建请求
WebRequest request = WebRequest.Create(url);
//发送请求,获取相应s
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream()) //获取响应的数据流
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("utf-8"))) //指定的数据流初始化为StreamReader 类
{
areaJson = reader.ReadToEnd(); //读取数据流中的所有字符
theData = JsonHelper.Instance.Deserialize<TaobaoData>(areaJson);
if (theData.code == "1") //表示获取数据失败
{
return null;
}
return theData;
}
}
}
catch
{
return null;
}
}
} public class TaobaoData
{
public string code { get; set; } public IPData data { get; set; }
} public class IPData
{
/// <summary>
/// 外网IP
/// </summary>
public string ip { get; set; } public string country { get; set; } public string area { get; set; } public string region { get; set; } public string city { get; set; } public string county { get; set; } /// <summary>
/// 运营商
/// </summary>
public string isp { get; set; } public string country_id { get; set; } public string area_id { get; set; } public string region_id { get; set; } public string city_id { get; set; } public string county_id { get; set; } public string isp_id { get; set; }
} }
上一篇:Linux sysinfo获取系统相关信息


下一篇:Spring AOP: 织入的顺序