0

Java 7 を使用して Java デスクトップ アプリケーションを作成しています。私のアプリケーションでは、POST でデータをサーバーに送信したいと考えています (HTTP を使用)。サーバーは、localhost のローカル マシンで実行されています。しかし、サーバーに接続しようとすると、接続のリセット (SocketTimeoutException) が返されます。接続できません。 http://www.google.deなどの Web ページにも接続しようとしましたが、失敗します。var 本体には、POST データが正しい形式で含まれています。(ファイアウォールを無効にして接続しようとしました)私のコード:

body=body.substring(0,body.length()-2);
HttpURLConnection connection = null;
try {
    if (revision){ //Connect to the revision server
        this.urlRevision = new URL(this.settingsRevision.getAddress());
        connection = (HttpURLConnection) urlRevision.openConnection();
        connection.setRequestMethod("POST");
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(10000);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", String.valueOf(body.length()));
        connection.connect();
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(body);
        writer.flush();

        this.returnedData = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        for(String line; (line = returnedData.readLine()) != null;){
            System.out.println(line);
        }
        writer.close();
        this.returnedData.close();
    }
} catch (Exception e) {
        this.exception=e;
}
4

2 に答える 2

0

次のように接続を閉じてみてください。

} finally {
    if(connection != null) {
      connection.disconnect(); 
    }
}
于 2013-01-20T23:43:42.273 に答える