2010-02-16 3 views
5

whatismyip.com에서 내 고객 IP가 필요합니다. 하지만 정규식 패턴이 맞지 않을까 생각해? 이 패터턴을 도와 줄 수있어?.NET으로 클라이언트 IP를 어떻게 배울 수 있습니까?

+2

왜 whatismyip.com을 사용하여 IP를 검색 하시겠습니까? :) – Younes

+1

투표를 닫습니다. 같은 질문을 두 번씩하지 마십시오. 질문에 답을 얻지 못하면 자세한 내용으로 확대 해보십시오. http://stackoverflow.com/questions/2272483/how-can-i-get-ip-address-of-my-3g-modem –

+0

가치가있는 부분에 대해서는 본인의 원래 질문에 대한 내 대답이이 트릭을 수행해야한다고 생각합니다. http://stackoverflow.com/questions/2272483/how-can-i-get-ip-address-of-my-3g-modem/2272673#2272673 –

답변

3

이 www.whatismyip.com에서 자동화 인터페이스를 사용하는 방법보다 쉽게 ​​달성 할 수있다, 그래서 어떤 정규식에 대한 필요가 없습니다 :

static void Main(string[] args) 
    { 
     const string url = "http://www.whatismyip.com/automation/n09230945.asp"; 

     var client = new WebClient(); 
     try 
     { 
      var myIp = client.DownloadString(url); 
      Console.WriteLine("Your IP: " + myIp); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Error contacting website: " + ex.Message); 
     } 
    } 
+0

좋은 좋은 짧은 하나 –

0

사용하려고 시도하십시오.
http://www.whatismyip.org/
훨씬 간단합니다.

또는 정확히 whatismyip.com 정보를 구문 분석 하시겠습니까?

+0

나는 그들이 이미 그것에 대해 논쟁하고 있다고 생각한다. 당신은 늦었습니다 ....... –

0

대신 이런 식으로 작업을 수행 :

class Program 
{ 
    static void Main(string[] args) 
    { 
     string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp"; 
     WebClient wc = new WebClient(); 
     UTF8Encoding utf8 = new UTF8Encoding(); 
     string requestHtml = ""; 
     try 
     { 
      requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); 
     } 
     catch (WebException we) 
     { 
      // do something with exception 
      Console.Write(we.ToString()); 
     } 

     IPAddress externalIp = null; 
     externalIp = IPAddress.Parse(requestHtml); 

     Console.Write("IP Numaram:" + externalIp.ToString()); 
     Console.ReadKey(); 

    } 
} 
4

당신은 얻을 HTML에서 주석을 읽게

www.whatismyip.com/로부터 IP를 긁어 코드를 설정하십시오 자동화/n09230945.asp 자세한 내용은 포럼의 "Recommended Automation Practices" 스레드를 참조하십시오.

그래서 이것은 당신이 가야한다 :

using (var client = new WebClient()) 
{ 
    Console.WriteLine(client.DownloadString(
     "http://www.whatismyip.com/automation/n09230945.asp")); 
} 
+0

사용하면 자동으로 변수에 의해 사용되는 리소스를 삭제하는 좋은 기술입니다 .. –

0

이것은 ASP.NET C에서 IP를 얻는 방법입니다.

string pstrClientAddress = HttpContext.Current.Request.UserHostAddress; 
관련 문제