Java rest webservice客户端

请帮我创建一个休息客户端以进行以下休息服务 .

endpoints 网址:http://xxxyyy.com

Headers :内容类型:application / x-www-form-urlencoded; charset = UTF-8

Headers :授权:基本:Base64encoded用户名和密码

正文:grant_type =密码&用户名= xxx&密码= yyy

非常感谢您的帮助 .

回答(1)

3 years ago

我试过下面,不知道在哪里添加第二个 Headers 和正文 .

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);