2

簡単な質問:

複数の httpURLConnection を同時にリクエストすることは可能ですか? 特定のサーバーにページが存在するかどうかを確認するツールを作成していますが、現時点では、Java は httpURLConnection が終了するたびに新しいページを開始するまで待機しているようです。これが私のコードです:

public static String GetSource(String url){
String results = "";
try{
  URL SourceCode = new URL(url);
  URLConnection connect = SourceCode.openConnection();
  connect.setRequestProperty("Host", "www.someserver.com");
  connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0");
  connect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  connect.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  connect.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  connect.setRequestProperty("Keep-Alive", "115");
  connect.setRequestProperty("Connection", "keep-alive");
  BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream(), "UTF-8"));
  String inputLine;
  while ((inputLine = in.readLine()) != null){
    results += inputLine;
  }
  return results;
}catch(Exception e){
  // Something's wrong
}
return results;
}

どうもありがとう!

4

2 に答える 2

1

ヒットごとにスレッドを作成する必要があります。Runnableを実装するクラスを作成し、すべての接続コードをrunメソッド内に配置します。

次に、このようなもので実行します...

for(int i=0; i < *thread count*; i++){
    Thread currentThread = new Thread(*Instance of your runnable Class*);
    currentThread.start();
}
于 2012-04-18T17:48:14.803 に答える
1

はい、可能です。投稿したコードは、複数のスレッドから同時に呼び出すことができます。

于 2012-04-18T17:27:19.683 に答える