2015-10-26 125 views
1

我知道这个问题已被问及一百万次,但我无法弄清楚我的令牌请求有什么问题。泽西岛的卷发令牌请求等效

在卷曲的命令是

卷曲-v -X POST -H “授权:基本XXXXXXXXXXXXXXXXXXXXXX” -H “内容类型:应用/ X WWW的窗体-urlencoded;字符集= UTF -8" -k -d “grant_type =密码&用户名= XXXXX &密码= XXXXX” https://localhost/sso/token

转换为球衣应该是:

Client client = Client.create(); 
WebResource webResource = client.resource("https://localhost/sso/token"); 
String appKey= "Basic XXXXXXXXXXXXXXXXXXXXXX" 
String input="grant_type=password&username=XXXXX&password=XXXXX"; 
    ClientResponse response = null; 
      response = webResource. 
        header("Authorization", appKey). 
        header("Content-Type", "/x-www-form-urlencoded;charset=UTF-8"). 
        accept("application/json"). 
        post(ClientResponse.class, input); 




    if (response.getStatus() != 200) { 
        throw new RuntimeException("Failed : HTTP error code : " 
          + response.getStatus()); 
       } 
       String output = response.getEntity(String.class); 
       System.out.println("Server response .... \n"); 
       System.out.println(output); 
      } catch (Exception e) { 
       e.printStackTrace(); 
     }** 

答案我得到:

了java.lang.RuntimeException:失败:HTTP错误代码:415 com.javacodegeeks.enterprise.rest.jersey.jerseyclient.JerseyClientAccessToken.main(JerseyClientAccessToken.java :67)

有人可以告诉我我做错了什么吗?

回答

1

此:

header("Content-Type", "application/json;charset=UTF-8"); 

不一样的是:

Content-Type: "application/x-www-form-urlencoded;charset=UTF-8" 

所以,你应该改变它。这就是为什么你得到一个415错误(不支持的媒体类型)。

+1

嘿,谢谢你的帮助! – MultiTask70