3

タイトルにあるように...ユーザーがJavaSwingアプリケーションのボタンをクリックしたときに、次のコードを使用してPHPスクリプトを実行しようとしました。

URL url = new URL( "http://www.mywebsite.com/my_script.php" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();

しかし、何も起こりません...何か問題がありますか?

4

2 に答える 2

6

次のようなステップが欠けていると思います。

InputStream is = conn.getInputStream();

HttpURLConnection基本的には、電話をかけるなどconnectの必要なことを行うために、ソケットを開くだけです。getInputStream()getResponseCode()

URL url = new URL( "http://google.com/" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
    InputStream is = conn.getInputStream();
    // do something with the data here
}else{
    InputStream err = conn.getErrorStream();
    // err may have useful information.. but could be null see javadocs for more information
}
于 2010-10-26T12:20:24.970 に答える
1
final URL url = new URL("http://domain.com/script.php");
final InputStream inputStream = new InputStreamReader(url);
final BufferedReader reader = new BufferedReader(inputStream).openStream();

String line, response = "";

while ((line = reader.readLine()) != null)
{
    response = response + "\r" + line;
}

reader.close();

「応答」はページのテキストを保持します。キャリッジリターンを試してみることをお勧めします(OSによっては、\ n、\ r、または両方の組み合わせを試してください)。

お役に立てれば。

于 2010-10-26T12:30:17.910 に答える