美文网首页
获取本机外网的ip,C#

获取本机外网的ip,C#

作者: 价值投机168 | 来源:发表于2022-06-05 09:20 被阅读0次
       /// <summary>
        /// 获取全部信息
        /// </summary>
        /// <returns>一段网页代码</returns>
        static 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;
        }
    
    
        static Dictionary<string, object> KeyValueCombination(string str)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(str);
            return json;
        }
    
    
        static string getOutIp()
        {
            Dictionary<string, object> response = getOutMessage();
            if (response != null)
            {
                return response["cip"].ToString();
            }
            else
            {
                return null;
            }
        }

    相关文章

      网友评论

          本文标题:获取本机外网的ip,C#

          本文链接:https://www.haomeiwen.com/subject/zzcrmrtx.html