请帮助我为以下休息服务创建一个休息客户端。
端点网址:http://xxxyyy.com
标题:内容类型:application / x-www-form-urlencoded; charset = UTF-8
标题:授权:基本:Base64encoded用户名和密码
正文:grant_type =密码&用户名= xxx&密码= yyy
感谢您的帮助。
答案 0 :(得分:-1)
我在下面尝试过,不知道在哪里添加第二个标题和正文。
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);