3

読んだ後: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を確実に取得する方法はありますか?

編集:大丈夫なので拒否されます、他の誰かが同じ機能を実行できる別のサイトを知っています

4

9 に答える 9

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);
}
于 2012-04-25T20:39:59.453 に答える
8

Go で遊んでいるときに、あなたの質問を見ました。Go を使用して Google App Engine で簡単なアプリを作成しました。

この URL にアクセスします。

http://agentgatech.appspot.com/

Java コード:

new BufferedReader(new InputStreamReader(new URL('http://agentgatech.appspot.com').openStream())).readLine()

コピーして独自のアプリを作成できるアプリのコードに移動します。

package hello

import (
    "fmt"
    "net/http"
)

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

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, r.RemoteAddr)
}
于 2012-04-25T21:00:33.513 に答える
4

一部のサーバーには、「ブラウザ以外」からのアクセスをブロックするトリガーがあります。彼らは、あなたがDOS 攻撃を実行できるある種の自動アプリであることを理解しています。これを回避するには、lib を使用してリソースにアクセスし、「browser」ヘッダーを設定してみてください。

wget は次のように機能します。

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

Java を使用すると、HttpClientライブラリを使用して「User-Agent」ヘッダーを設定できます。「試してみること」セクションのトピック 5 を見てください。

これがあなたを助けることを願っています。

于 2012-04-25T19:45:34.910 に答える
3

403応答は、サーバーが何らかの理由でリクエストを明示的に拒否していることを示します。詳細については、WhatIsMyIPのオペレーターにお問い合わせください。

于 2012-04-25T19:35:30.110 に答える
3

私たちはセットアップをCloudFlare行い、設計どおり、なじみのないユーザー エージェントに挑戦しています。UA を一般的なものに設定できれば、アクセスできるはずです。

于 2012-05-01T14:10:49.617 に答える
2

このような別のWebサービスを使用できます。http://freegeoip.net/static/index.html

于 2012-04-25T19:36:35.840 に答える