我知道这个问题已被问了一百万次,但我无法弄清楚我的令牌请求有什么问题。
curl中的命令是
curl -v -X POST -H“授权:基本XXXXXXXXXXXXXXXXXXXXXX”-H “内容类型:application / x-www-form-urlencoded; charset = 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 at com.javacodegeeks.enterprise.rest.jersey.jerseyclient.JerseyClientAccessToken.main(JerseyClientAccessToken.java:67)
有人可以告诉我我做错了什么吗?
答案 0 :(得分:1)
此:
header("Content-Type", "application/json;charset=UTF-8");
与此不同:
Content-Type: "application/x-www-form-urlencoded;charset=UTF-8"
所以你应该改变它。这就是您收到415错误(不支持的媒体类型)的原因。