2017-06-12 124 views
-3

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

端点URL:http://xxxyyy.com

部首:内容类型:application/X WWW的窗体-urlencoded;字符集= UTF-8

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

正文:grant_type =密码& username = xxx & password = yyy

您的帮助表示赞赏。

+0

您想让我们为您编写完整的代码? – Akshay

+1

先试试自己并带来障碍 – FieryCat

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

什么是客户端,WebSource,ClientResponse? –