专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C#在服务端如何获取外网IP

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:15:15       共计:3667 浏览

解决方案:


Dictionary<string, object> KeyValueCombination(string str)

       {

           JavaScriptSerializer serializer = new JavaScriptSerializer();

           Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(str);

           return json;

       }

       /// <summary>

       /// 获取全部信息

       /// </summary>

       /// <returns>一段网页代码</returns>

       Dictionary<string, object> getOutMessage()

       {

           WebClient client = new WebClient();

           client.Encoding = System.Text.Encoding.Default;

           string response = client.UploadString("http://pv.sohu.com/cityjson?ie=utf-8", "");

           Dictionary<string, object> obj =null;

           if(response!=null)

           {

               response = response.Substring(response.IndexOf("{"), (response.IndexOf("}")- response.IndexOf("{"))+1);

               obj=KeyValueCombination(response);

           }

           return obj;

       }

       /// <summary>

       /// 外网IP

       /// </summary>

       /// <returns>外网IP地址</returns>

       string getOutIp()

       {

           Dictionary<string, object> response = getOutMessage();

           if (response != null)

           {

               return response["cip"].ToString();

           }

           else

           {

               return null;

           }

       }

       /// <summary>

       /// 城市

       /// </summary>

       /// <returns>城市</returns>

       string getOutCity()

       {

           Dictionary<string, object> response = getOutMessage();

           

           if (response != null)

           {

               return response["cname"].ToString();

           }

           else

           {

               return null;

           }

       }

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C#开发中如何确定IP地址是否属于某个国家/地区 | ·下一条:C#下载图片

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有