纳金网

标题: C#获取本机外网ip [打印本页]

作者: 王者再临    时间: 2014-12-31 22:53
标题: C#获取本机外网ip
  1. using System;

  2. using System.Net;

  3. using System.Text.RegularExpressions;

  4. namespace ConsoleApplication1

  5. {

  6. class Program

  7. {

  8. static void Main(string[] args)

  9. {

  10. try

  11. {

  12. WebClient client = new WebClient();

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

  14. string response = client.UploadString(“http://iframe.ip138.com/ipcity.asp”, “”);

  15. Match mc = Regex.Match(response, @“location.href=”“(.*)”“”);

  16. if (mc.Success && mc.Groups.Count > 1)

  17. {

  18. response = client.UploadString(mc.Groups[1].Value, “”);

  19. string[] str1 = response.Split('[');

  20. response = str1[1];

  21. string [] str = response.Split(']');

  22. response = str[0];

  23. Console.Write(response);

  24. }

  25. }

  26. catch (System.Exception e)

  27. {

  28. }

  29. Console.Read();

  30. }

  31. }

  32. }
复制代码

作者: tianhett    时间: 2015-1-1 22:41
涨姿势了。。。。。。。。
作者: KISS2015    时间: 2015-1-2 19:21
ip138网站只有国内能访问吧
作者: 我不再年轻    时间: 2015-1-3 11:08
国外可以用whatismyipaddress.com




欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5