C#服务端如何获取外网IP的解决方案:
Dictionary<string, object> KeyValueCombination(string str)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(str);
return json;
}
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;
}
string getOutIp()
{
Dictionary<string, object> response = getOutMessage();
if (response != null)
{
return response["cip"].ToString();
}
else
{
return null;
}
}
string getOutCity()
{
Dictionary<string, object> response = getOutMessage();
if (response != null)
{
return response["cname"].ToString();
}
else
{
return null;
}
}
|