发布:2022/12/21 13:45:15作者:管理员 来源:本站 浏览次数:560
解决方案:
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;
}
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4