4

を使用して、tinkerpop データベースを実験する REST クライアントに取り組んでいますHttpURLConnection

を送信しようとしています'GET - CONNECT'。今、私は(いくつかのネット調査から)doOutput(true)「クライアント」を使用すると「POST」setRequestMethod 'GET'がデフォルトであっても「POST」になることを理解しています(まあ大丈夫ですか?)しかし、コメントアウトすると、doOutput(true)このエラーが発生します:

java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:995)
at RestContent.handleGetConnect(RestContent.java:88)

at RestClient.main(RestClient.java:42)`

これは、さまざまなオプションを試した通信コードの抜粋ですsetUseDoOutPut()

//connection.setDoInput(true);
connection.setUseCaches (false);
connection.setDoOutput(true);
connection.setAllowUserInteraction(false);

// set GET method 
try {
    connection.setRequestMethod("GET");
} catch (ProtocolException e1) {
    e1.printStackTrace();
    connection.disconnect();
}

connection.setRequestMethod("GET")それ以外の場合は例外です。ヒントはありますか?

4

1 に答える 1

1

次のコードは問題なく動作します: URL は、GET 操作がサポートされている REST URL です。

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    //connection.setDoOutput(true);
    InputStream content = (InputStream) connection.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(content));
    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
于 2013-09-26T09:40:33.163 に答える