java - How to disable chunked encoding and use buffered for Jersey 1? -
i encountering http response status of 411 length required when performing call 3rd party service in application.
the problem seems related chuncked encoding
, switching buffered
solve problem. there solution here jersey2: jersey http client replying 411 on post request
however due legacy, using jersey1 in our application , can not perform migration in close future.
i tried isolate problem. here minimal code:
import com.sun.jersey.api.client.client; import com.sun.jersey.api.client.clientresponse; import com.sun.jersey.api.client.webresource; import com.sun.jersey.api.client.config.clientconfig; import com.sun.jersey.api.client.config.defaultclientconfig; import com.sun.jersey.api.json.jsonconfiguration; /** * main */ public class main { public static void main(string[] args) { clientconfig clientconfig = new defaultclientconfig(); clientconfig.getfeatures().put(jsonconfiguration.feature_pojo_mapping, boolean.true); clientconfig.getproperties().put(clientconfig.property_chunked_encoding_size, null); client client = client.create(clientconfig); string posturl = "https://api.napster.com/v2.2/me/library/tracks?id=tra.169783383,tra.30621111"; webresource webresourcepost = client.resource(posturl); webresourcepost.header("content-length", 0); clientresponse response = webresourcepost.type("application/json").post(clientresponse.class); system.out.println(response.getstatus()); } }
here build.gradle:
apply plugin: 'application' apply plugin: 'java' repositories { mavencentral() mavenlocal() } mainclassname = "main" dependencies { testcompile group: 'junit', name: 'junit', version: '4.11' compile group: 'com.sun.jersey.contribs', name: 'jersey-apache-client', version: '1.19.4' compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.4' }
what have tried add configuration switch buffered call:
clientconfig.getproperties().put(clientconfig.property_chunked_encoding_size, null);
i quote documentation of property property_chunked_encoding_size
if property absent chunked encoding not used. value < = 0 declares chunked encoding used default chunk size. value > 0 declares chunked encoding used value declared chunk size.
even if set property still receive http response status of 411 length required.
later edit: call not leave the local machine. when expected receive 401 unauthorized, since need add header auth key. yet not focus of question, if considers better add details on how obtain this, please leave comment , describe more.
to start
you not have set flag clientconfig.property_chunked_encoding_size
, please check here or in quote above.
jersey client default set content-type
application/json
(by nature), should have passed blank json object content-lenght
correct passed in http request.
thus work, should post
blank json
object without setting content-lenght
header.
here working code, http 401
import com.sun.jersey.api.client.client; import com.sun.jersey.api.client.clientresponse; import com.sun.jersey.api.client.webresource; import com.sun.jersey.api.client.config.clientconfig; import com.sun.jersey.api.client.config.defaultclientconfig; import com.sun.jersey.api.client.filter.loggingfilter; import com.sun.jersey.api.json.jsonconfiguration; /** * main */ public class main { public static void main(string[] args) { clientconfig clientconfig = new defaultclientconfig(); clientconfig.getfeatures().put(jsonconfiguration.feature_pojo_mapping, boolean.true); //clientconfig.getproperties().put(clientconfig.property_chunked_encoding_size, null); client client = client.create(clientconfig); client.addfilter(new loggingfilter(system.out)); string posturl = "https://api.napster.com/v2.2/me/library/tracks?id=tra.169783383,tra.30621111"; webresource webresourcepost = client.resource(posturl); //webresourcepost.header("content-length", 0); clientresponse response = webresourcepost.type("application/json").post(clientresponse.class, "{}"); system.out.println(response.getstatus()); } }
Comments
Post a Comment