android - HTTP Post Only works with default HttpClient, not with OkHttpClient -
i'm working on project requires me send post request http://sagecell.sagemath.org/kernel (just post, no data)along 2 headers, accept-encoding:identity , accepted_tos:true.
this works fine if using default httpclient so:
httpclient = new defaulthttpclient(); httppost httppost = new httppost(); string url = urlutils.getkernelurl(); httppost.seturi(uri.create(url)); arraylist<namevaluepair> postparameters = new arraylist<namevaluepair>(); postparameters.add(new basicnamevaluepair(header_accept_encoding,value_identity)); postparameters.add(new basicnamevaluepair(header_tos,"true")); httppost.setentity(new urlencodedformentity(postparameters)); httpresponse httpresponse = httpclient.execute(httppost); inputstream inputstream = httpresponse.getentity().getcontent(); websocketresponse = gson.fromjson(new inputstreamreader(inputstream), websocketresponse.class); inputstream.close();
however if want use same thing using okhttpclient, gives me 403 error:
httpclient = new okhttpclient(); public static final mediatype jsonmediatype = mediatype.parse("application/json; charset=utf-8"); requestbody body = requestbody.create(jsonmediatype, ""); request request = new request.builder() .addheader(header_accept_encoding, value_identity) .addheader(header_tos, "true") .url(url) .post(body) //i've tried null here .build(); response response = httpclient.newcall(request).execute(); log.i(tag,"status code"+response.code()); //this 403
this same story libraries ion , httpurlconnection, apache client seems work.
any answers why isn't working appreciated.
error 403 means forbidden server. , in first case(while using default httpclient ) not adding header, adding name-value pair entity. add header should use
httppost.addheader("key","value");
Comments
Post a Comment