2

SunのJavaチュートリアルのコードを使用します

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

スタックトレースは、接続がタイムアウトしたのと同じです。なんで?

ファイアウォールに問題があるのではないかと思いますが

  1. google.comへのpingは大丈夫です
  2. ブラウザで動作します
  3. このアプローチは、私が提供するすべてのURLで失敗します
  4. 他のプログラムでDJWebBrowserコンポーネントを使用していますが、ブラウザとしては問題なく動作します

この問題について詳しく調べるにはどうすればよいですか?コードを実行するときに使用されるポート番号を知ることはできますか?

ありがとう

4

2 に答える 2

4

会社で使用されているプロキシを見つけて、プログラムに設定します。[1] からのコード引用

//Set the http proxy to webcache.mydomain.com:8080

System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setPropery("http.proxyPort", "8080");

// Next connection will be through proxy.
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();

// Now, let's 'unset' the proxy.
System.setProperty("http.proxyHost", null);

// From now on http connections will be done directly.

[1] - http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

于 2012-10-18T15:10:06.360 に答える