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