0

これは単なる演習です。このコードに相当するものがあるかどうか疑問に思っています:

String https_url = "https://api.ciscospark.com/v1/rooms";
URL url = new URL(https_url);
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();

connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 
connection.setRequestProperty("Authorization", "I3MDUtMmEy");
connection.setDoOutput(true);

次のようなものを作ることができると思いました:

String https_url = "https://api.ciscospark.com/v1/rooms";
        URL url = new URL(https_url);
        HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
        connection.setDoOutput(true);
        PrintWriter pw = new PrintWriter(connection.getOutputStream());
        pw.println("GET /v1/rooms HTTP/1.1");
        pw.println("Host: https://api.ciscospark.com");
        pw.println("Content-Type: application/json; charset=utf-8");
        pw.println("Authorization: I3MDUtMmEy");
        pw.println("");

しかし、サーバーから認証エラー メッセージが返され、HTTP 応答コードが返されました: URL の 401: https://api.ciscospark.com/v1/rooms 401 認証資格情報が見つからないか、正しくありません。printwriter を使用して動作させることはできますか? そうでない場合、なぜですか?ありがとう

4

1 に答える 1