我有一个用于在 UNIX 中执行的 Curl 命令。
curl --digest -u 'user':'pass' 'https://<URL>/getCustomers' (which is working for me)
我试图用Java来做到这一点。
我尝试使用 Restlet 和 HttpClient,但都没有。我不知道实现它的确切方法。
private static void get() {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("https://<URL>/getCustomers");
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(USER, PASSWORD));
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
不知道哪里做错了,各位大神能帮帮我吗?输出是 JSON,你们也可以告诉我如何捕获输出吗?