C# asp.net获取外网IP
2011年12月01日
#region 获取外网IP
//获取外网IP
public static string getReaderIpds()
{
string pubIP = "";
string serviceUrl = "";
Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sk.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
string req = "M-SEARCH * HTTP/1.1\r\n" +
"HOST: 239.255.255.250:1900\r\n" +
"ST:upnp:rootdevice\r\n" +
"MAN:\"ssdp:discover\"\r\n" +
"MX:3\r\n\r\n";
byte[] data = System.Text.Encoding.ASCII.GetBytes(req);
byte[] receive = new byte[0x1000];
IPEndPoint ipe = new IPEndPoint(IPAddress.Broadcast, 1900);
//发送数据,获取服务地址
sk.SendTo(data, ipe);
int length = 0;
length = sk.Receive(receive);
string resp = Encoding.ASCII.GetString(receive, 0, receive.Length).ToLower();
if (resp.Contains("upnp:rootdevice"))
{
resp = resp.Substring(resp.ToLower().IndexOf("location:") + 9);
resp = resp.Substring(0, resp.IndexOf("\r")).Trim();
serviceUrl = GetServiceUrl(resp);//解析获取地址
}
//利用ssdp discover服务向serviceIP发送请求,存储返回结果,并在xml中解析出外网ip
string reqStr = "" +
"" +
"" +
"" + "" +
"" +
"";
byte[] b = Encoding.UTF8.GetBytes(reqStr);
WebRequest wr = HttpWebRequest.Create(serviceUrl);
wr.Method = "POST";
wr.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:WANIPConnection:1#" + "GetExternalIPAddress" + "\"");
wr.ContentType = "text/xml; charset=\"utf-8\"";
wr.ContentLength = b.Length;
wr.GetRequestStream().Write(b, 0, b.Length);
//
XmlDocument xml = new XmlDocument();
WebResponse ws = wr.GetResponse();
Stream ress = ws.GetResponseStream();
xml.Load(ress);
XmlNamespaceManager xmlMgr = new XmlNamespaceManager(xml.NameTable);
xmlMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
pubIP = xml.SelectSingleNode("//NewExternalIPAddress/text()", xmlMgr).Value;
return pubIP;
}
///
/// 剖析
///
///
///
private static string GetServiceUrl(string resp)
{
try
{
XmlDocument desc = new XmlDocument();
desc.Load(WebRequest.Create(resp).GetResponse().GetResponseStream());
XmlNamespaceManager nsMgr = new XmlNamespaceManager(desc.NameTable);
nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
XmlNode typen = desc.SelectSingleNode("//tns:device/tns:deviceType/text()", nsMgr);
if (!typen.Value.Contains("InternetGatewayDevice"))
return null;
XmlNode node = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:controlURL/text()", nsMgr);
if (node == null)
return null;
XmlNode eventnode = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:eventSubURL/text()", nsMgr);
int n = resp.IndexOf("://");
n = resp.IndexOf('/', n + 3);
string surl = resp.Substring(0, n) + eventnode.Value;
return surl;
}
catch
{
return null;
}
}
//获取外网IP
private static string GetExternalIP()
{
return "";
//以下是通过外网页面读取的
//try
//{
// System.Net.WebClient client = new System.Net.WebClient();
// client.Encoding = System.Text.Encoding.Default;
// string reply = client.DownloadString("http://www.ip138.com/ip2city.asp");
// string[] ipStr = reply.Split(new char[] { '[', ']' });
// pubIP = ipStr[1];
//}
//catch (Exception ex)
//{
// System.Windows.Forms.MessageBox.Show(ex.Message);
//}
}
#endregion
2011年12月01日
#region 获取外网IP
//获取外网IP
public static string getReaderIpds()
{
string pubIP = "";
string serviceUrl = "";
Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sk.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
string req = "M-SEARCH * HTTP/1.1\r\n" +
"HOST: 239.255.255.250:1900\r\n" +
"ST:upnp:rootdevice\r\n" +
"MAN:\"ssdp:discover\"\r\n" +
"MX:3\r\n\r\n";
byte[] data = System.Text.Encoding.ASCII.GetBytes(req);
byte[] receive = new byte[0x1000];
IPEndPoint ipe = new IPEndPoint(IPAddress.Broadcast, 1900);
//发送数据,获取服务地址
sk.SendTo(data, ipe);
int length = 0;
length = sk.Receive(receive);
string resp = Encoding.ASCII.GetString(receive, 0, receive.Length).ToLower();
if (resp.Contains("upnp:rootdevice"))
{
resp = resp.Substring(resp.ToLower().IndexOf("location:") + 9);
resp = resp.Substring(0, resp.IndexOf("\r")).Trim();
serviceUrl = GetServiceUrl(resp);//解析获取地址
}
//利用ssdp discover服务向serviceIP发送请求,存储返回结果,并在xml中解析出外网ip
string reqStr = "" +
"" +
"" +
"" + "" +
"" +
"";
byte[] b = Encoding.UTF8.GetBytes(reqStr);
WebRequest wr = HttpWebRequest.Create(serviceUrl);
wr.Method = "POST";
wr.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:WANIPConnection:1#" + "GetExternalIPAddress" + "\"");
wr.ContentType = "text/xml; charset=\"utf-8\"";
wr.ContentLength = b.Length;
wr.GetRequestStream().Write(b, 0, b.Length);
//
XmlDocument xml = new XmlDocument();
WebResponse ws = wr.GetResponse();
Stream ress = ws.GetResponseStream();
xml.Load(ress);
XmlNamespaceManager xmlMgr = new XmlNamespaceManager(xml.NameTable);
xmlMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
pubIP = xml.SelectSingleNode("//NewExternalIPAddress/text()", xmlMgr).Value;
return pubIP;
}
///
/// 剖析
///
///
///
private static string GetServiceUrl(string resp)
{
try
{
XmlDocument desc = new XmlDocument();
desc.Load(WebRequest.Create(resp).GetResponse().GetResponseStream());
XmlNamespaceManager nsMgr = new XmlNamespaceManager(desc.NameTable);
nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
XmlNode typen = desc.SelectSingleNode("//tns:device/tns:deviceType/text()", nsMgr);
if (!typen.Value.Contains("InternetGatewayDevice"))
return null;
XmlNode node = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:controlURL/text()", nsMgr);
if (node == null)
return null;
XmlNode eventnode = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:eventSubURL/text()", nsMgr);
int n = resp.IndexOf("://");
n = resp.IndexOf('/', n + 3);
string surl = resp.Substring(0, n) + eventnode.Value;
return surl;
}
catch
{
return null;
}
}
//获取外网IP
private static string GetExternalIP()
{
return "";
//以下是通过外网页面读取的
//try
//{
// System.Net.WebClient client = new System.Net.WebClient();
// client.Encoding = System.Text.Encoding.Default;
// string reply = client.DownloadString("http://www.ip138.com/ip2city.asp");
// string[] ipStr = reply.Split(new char[] { '[', ']' });
// pubIP = ipStr[1];
//}
//catch (Exception ex)
//{
// System.Windows.Forms.MessageBox.Show(ex.Message);
//}
}
#endregion