我在下面試過,不知道在哪裏添加第二個標題和正文。
String url = "http://xxx";
String name = "xxx";
String password = "yyy";
String authString = name + ":" + password;
String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
System.out.println("Base64 encoded auth string: " + authStringEnc);
Client restClient = Client.create();
WebResource webResource = restClient.resource(url);
ClientResponse resp = webResource.accep`enter code here`t("application/json")
.header("Authorization", "Basic " + authStringEnc)
.get(ClientResponse.class);
if(resp.getStatus() != 200){
System.err.println("Unable to connect to the server");
}
String output = resp.getEntity(String.class);
System.out.println("response: "+output);
您想讓我們爲您編寫完整的代碼? – Akshay
先試試自己並帶來障礙 – FieryCat
我發佈了我的方法 –