java Jersey 客户端使用 x-www-form-urlencoded 发布请求失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38437136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey client Post Request with x-www-form-urlencoded Fails
提问by VelNaga
Hi I am using Glassfish jersey-client to get oauth-Token from REST URL. I am able to get the token via postman client & CURL,please find the below image for reference,
嗨,我正在使用 Glassfish jersey-client 从 REST URL 获取 oauth-Token。我可以通过邮递员客户端和 CURL 获取令牌,请找到下图以供参考,
$ curl 'https://sample.com/oauth2/token' -X POST -d'g
rant_type=samples&id=2ZwqWBdksfjkads6Q8yNW3s58LNeOMucJeb&s
ecret=dkfflJTZOqA1GCEH&scope=GROUP'
But unable to achieve it via code,
但无法通过代码实现,
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.22.2</version>
</dependency>
I am using following code to get the token
我正在使用以下代码来获取令牌
Form form = new Form();
form.param("grant_type", "samples");
form.param("id", "2ZwqWBdksfjkads6Q8yNW3s58LNeOMucJeb");
form.param("secret", "HGoslJTZOqA1GCEH");
form.param("scope", "dkfflJTZOqA1GCEH");
JerseyClientBuilder jerseyClientBuilder = new JerseyClientBuilder()
.register(new LoggingFilter());
JerseyWebTarget jerseyWebTarget = jerseyClientBuilder.build().target(hostname);
response = jerseyWebTarget.request().accept(MediaType.APPLICATION_FORM_URLENCODED).post(Entity.form(form));
Keep on getting StatusCode=406(Not acceptable) as a response. Shall i passing the URL parameters properly?
继续获取 StatusCode=406(不可接受)作为响应。我应该正确传递 URL 参数吗?
I would highly appreciate if someone give me a hint to resolve this issue.
如果有人给我一个提示来解决这个问题,我将不胜感激。
采纳答案by Paul Samsotha
Get rid of this .accept(MediaType.APPLICATION_FORM_URLENCODED)
. This sets the Accept
header. You are saying here that you want a response with data type application/x-www-form-urlencoded
. The server doesn't know how to response with that type so it's telling you that that response type is not acceptable.
摆脱这个.accept(MediaType.APPLICATION_FORM_URLENCODED)
。这将设置Accept
标题。您在这里是说您想要一个数据类型为 的响应application/x-www-form-urlencoded
。服务器不知道如何使用该类型进行响应,因此它告诉您该响应类型是不可接受的。
What you want is to send the Content-Type
header, not the Accept
header. Using Entity.form(Form)
automatically sets the Content-Type
to application/x-www-form-urlencoded
so you really don't need to do anything else. Just remove the accept
method call.
您想要的是发送Content-Type
标头,而不是Accept
标头。使用Entity.form(Form)
自动设置Content-Type
为application/x-www-form-urlencoded
所以你真的不需要做任何其他事情。只需删除accept
方法调用。
UPDATE
更新
Seems the client is setting an Accept header the server doesn't like, so you can explicitly set the Accept
header to application/json
since that is the content-type sent back by the server for the token response.
似乎客户端正在设置服务器不喜欢的 Accept 标头,因此您可以显式设置Accept
标头,application/json
因为这是服务器为令牌响应发回的内容类型。
If you want to get the token as a Java object, you can just create a Token
class with all the JSON properties in the token
如果您想将令牌作为 Java 对象获取,您只需创建一个Token
包含令牌中所有 JSON 属性的类
public class Token {
@JsonProperty("access_token")
private String accessToken;
// other properties
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getAccessToken() {
return this.accessToken;
}
// other getters and setters
}
Then just do
然后就做
Token token = response.readEntity(Token.class);
If you don't know all the other properties in the token response, just look at the contents of the logging filter. You should see the response. But you need to configure the logging filter to show the body
如果您不知道令牌响应中的所有其他属性,只需查看日志过滤器的内容。您应该会看到响应。但是你需要配置日志过滤器来显示正文
.register(new LoggingFilter(Logger.getAnonymousLogger(), true));