Java rest webservice客户端

时间:2017-06-12 07:07:14

标签: java rest service client

请帮助我为以下休息服务创建一个休息客户端。

端点网址:http://xxxyyy.com

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

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

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

感谢您的帮助。

1 个答案:

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