C#获取本机外网ip

 using System;
  
   using System.Net;
  
   using System.Text.RegularExpressions;
  
  namespace ConsoleApplication 1
  
   {
  
   class Program
  
   {
  
  static void Main ( string [] args )
  
   {
  
   try
  
   {
  
  WebClient client = new WebClient ( ) ;
  
  client.Encoding = System.Text.Encoding.Default;
  
   string response = client.UploadString ( “http : / / iframe.ip 138. com / ipcity.asp” , “” ) ;
  
  Match mc = Regex.Match ( response , @“ location .href = ”“ ( . * ) ”“” ) ;
  
   if ( mc.Success & & mc.Groups.Count > 1 )
  
   {
  
  response = client.UploadString ( mc.Groups[ 1 ].Value , “” ) ;
  
   string [] str 1 = response.Split ( '[' ) ;
  
  response = str 1 [ 1 ];
  
   string [] str = response.Split ( ']' ) ;
  
  response = str[ 0 ];
  
  Console.Write ( response ) ;
  
   }
  
   }
  
  catch ( System.Exception e )
  
   {
  
   }
  
  Console.Read ( ) ;
  
   }
  
   }
  
   }

你可能感兴趣的:(C#获取本机外网ip)