4

I keep receiving a 406 HTTP response when I try to execute code structured in this way. I have tried restructuring the code and the inputs many times, but I still receive this error, and I've gotten to the point I don't even really know what to debug. The exception seems to indicate that the post() method isn't supplying the @FormParams in the desired format, but as you can see the .accept(MediaType.APPLICATION_FORM_URLENCODED) and the @Consumes(MediaType.APPLICATION_FORM_URLENCODED) do indeed match up.

I am using the Firefox add-on HTTPRequester to pass in the @FormParams and have ensured that I am passing them in with the appropriate Content-Type (application/x-www-form-urlencoded). I've run out of things to check. Does anyone have any ideas?


The Proxy Service

Client client = Client.create();
WebResource service = client.resource(myURL);

Form form = new Form();
form.add("value1", value1);
form.add("value2", value2);
form.add("valueN", valueN);

String returnValue = service.accept(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form);

The Actual Service

@POST
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/theService")
public String theService(
        @FormParam("value1") String value1,
        @FormParam("value2") String value2,
        @FormParam("valueN") String valueN) {

    String returnValue = null;

    /*
     * Do Stuff
     */

    return returnValue;
}

The Exception

com.sun.jersey.api.client.UniformInterfaceException: POST http://theURL/theService returned a response status of 406
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:563)
at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:69)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:499)
4

1 回答 1

9

UniformInterfaceException只是一个名称很差的包罗万象的异常(之所以这样命名,是因为它是一个提供统一接口的异常,无论错误如何)。它基本上是泽西岛的任何东西抛出的 IOException 。实际错误是406 Unacceptable

请求的资源只能生成根据请求中发送的 Accept 标头不可接受的内容。

在这里你说你接受MediaType.APPLICATION_FORM_URLENCODED

String returnValue = service.accept(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form);

但是您的服务会产生MediaType.APPLICATION_XML

@Produces(MediaType.APPLICATION_XML)

由于您的服务器无法生成客户端表示将接受的任何内容,因此它返回 406 错误。

最有可能的是,您的意思是设置WebResource.type,而不是accept

String returnValue = service.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form);
于 2012-09-19T20:43:19.753 回答