2012-04-25 177 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; 
}