2012-04-25 176 views
3

看完後:Getting the 'external' IP address in Java如何獲得外部IP成功

代碼:

public static void main(String[] args) throws IOException 
{ 
    URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream())); 

    String ip = in.readLine(); //you get the IP as a String 
    System.out.println(ip); 
} 

我認爲我是一個勝利者,但我得到以下錯誤

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://automation.whatismyip.com/n09230945.asp 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at java.net.URL.openStream(Unknown Source) 
at getIP.main(getIP.java:12) 

我想這是因爲服務器沒有足夠快速響應,是否有確保它會獲得外部IP?

編輯:好了,所以它的被拒,其他網站可以做同樣的功能

+0

爲HTTP 403錯誤代碼意味着禁止。然而,我可以在我的瀏覽器中訪問該網站,而不會有任何問題。 – twain249 2012-04-25 19:35:49

回答

8

之前運行下面的代碼來看看這個:http://www.whatismyip.com/faq/automation.asp

public static void main(String[] args) throws Exception { 

    URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp"); 
    URLConnection connection = whatismyip.openConnection(); 
    connection.addRequestProperty("Protocol", "Http/1.1"); 
    connection.addRequestProperty("Connection", "keep-alive"); 
    connection.addRequestProperty("Keep-Alive", "1000"); 
    connection.addRequestProperty("User-Agent", "Web-Agent"); 

    BufferedReader in = 
     new BufferedReader(new InputStreamReader(connection.getInputStream())); 

    String ip = in.readLine(); //you get the IP as a String 
    System.out.println(ip); 
} 
+1

由於這個評論已經過時了,從現在開始,在這個網站上檢查ip並不那麼容易。以上更多評論不再有效。 – Drachenfels 2013-02-16 20:04:38

+0

http://checkip.amazonaws.com爲我工作,而不是URL(「http://automation.whatismyip.com/n09230945.asp」); – 2017-03-25 16:21:18

3

403響應表示服務器明確拒絕您由於某種原因,請求別人知道。有關詳細信息,請聯繫WhatIsMyIP的運營商。

+2

可能與服務條款有關:http://www.whatismyip.com/faq/automation。asp – Flexo 2012-04-25 19:38:17

3

某些服務器具有觸發器,可以阻止來自「非瀏覽器」的訪問。他們知道你是某種自動應用程序,可以執行DOS attack。爲了避免這種情況,您可以嘗試使用lib來訪問資源並設置「瀏覽器」標題。在此way

wget的作品:

wget -r -p -U Mozilla http://www.site.com/resource.html 

使用Java,您可以使用HttpClient lib,並設置 「用戶代理」 標頭。 查看「要嘗試的事情」部分的主題5。

希望這可以幫助你。

8

雖然與去打打我看到你的問題。我做了一個快速應用程序在谷歌App Engine的使用轉到:

打這個網址:

http://agentgatech.appspot.com/

Java代碼:

new BufferedReader(new InputStreamReader(new URL('http://agentgatech.appspot.com').openStream())).readLine() 
對於您可以複製並應用

Go代碼您自己的應用程序:

package hello 

import (
    "fmt" 
    "net/http" 
) 

func init() { 
    http.HandleFunc("/", handler) 
} 

func handler(w http.ResponseWriter, r *http.Request) { 
    fmt.Fprint(w, r.RemoteAddr) 
} 
3

我們成立了CloudFlare和設計他們正在挑戰不熟悉的使用者。如果您可以將您的UA設置爲通用的,您應該可以獲得訪問權限。

10
public static void main(String[] args) throws IOException 
    { 
    URL connection = new URL("http://checkip.amazonaws.com/"); 
    URLConnection con = connection.openConnection(); 
    String str = null; 
    BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
    str = reader.readLine(); 
    System.out.println(str); 
    } 
1

使用AWS上檢查IP地址的鏈接工作了me.Please注意MalformedURLException的,IOException異常將被添加以及

public String getPublicIpAddress() throws MalformedURLException,IOException { 

    URL connection = new URL("http://checkip.amazonaws.com/"); 
    URLConnection con = connection.openConnection(); 
    String str = null; 
    BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
    str = reader.readLine(); 


    return str; 
}