这只是一个练习。我想知道是否有与此代码等效的代码:
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 使其工作吗?如果不是,为什么?谢谢