0

Why am I getting

ProtocolException: content-length promised 343 bytes, but received 0

in Android 4.x when using URLConnection? while works fine in Android 2.3.3...

Stacktrace:

W/System.err(7560): java.net.ProtocolException: content-length promised 343 bytes, but received 0
W/System.err(7560):     at com.android.okhttp.internal.http.RetryableOutputStream.close(RetryableOutputStream.java:52)
W/System.err(7560):     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:636)
W/System.err(7560):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
W/System.err(7560):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err(7560):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
W/System.err(7560):     at application._Utils.postFeedback(_Utils.java:95)
W/System.err(7560):     at application.OptionsPreference.onClick(OptionsPreference.java:559)
W/System.err(7560):     at android.view.View.performClick(View.java:4446)
W/System.err(7560):     at android.view.View$PerformClick.run(View.java:18437)
W/System.err(7560):     at android.os.Handler.handleCallback(Handler.java:733)
W/System.err(7560):     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err(7560):     at android.os.Looper.loop(Looper.java:136)
W/System.err(7560):     at android.app.ActivityThread.main(ActivityThread.java:5372)
W/System.err(7560):     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(7560):     at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err(7560):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
W/System.err(7560):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
W/System.err(7560):     at dalvik.system.NativeStart.main(Native Method)

Code:

final URLConnection _uc = new URL(url).openConnection();
final String parameters = String.format("fullname=%s&email=%s&website=&comment=%s&settings_WITH_JS=1&commentJsError=&hide_mail=0", name, email, message);
_uc.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
_uc.setRequestProperty("Cache-Control:", "no-cache");
_uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*");
_uc.setRequestProperty("Origin", "http://x.com");
_uc.setRequestProperty("Upgrade-Insecure-Requests", "1");
_uc.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) "); // Fixes FileNotFoundException in some cases.
_uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
_uc.setRequestProperty("Referer", referer);
_uc.setRequestProperty("Accept-Encoding", "gzip, deflate, lzma");
_uc.setRequestProperty("Accept-Language", "en-US,en;");
_uc.setRequestProperty("Accept-Charset", encoding); // Parameters encoding.
_uc.setConnectTimeout(timeout);
_uc.setReadTimeout(timeout);
_uc.setUseCaches(false);
_uc.setDoInput(true); // false = Ignores response body, and disallows use of getResponseCode().
_uc.setDoOutput(true); // true = POST/Put, and allows use of getOutputStream().
_uc.getOutputStream().write(parameters.getBytes(encoding)); // Put/POST.
_uc.getInputStream().close(); // Throws ProtocolException in Android +4!?

I've searched a lot but couldn't find anything... what's the problem?

Yousha Aleayoub
  • 3,221
  • 2
  • 42
  • 58
  • `_uc.setRequestProperty("Content-Length", Integer.toString(parameters.length()));` gives you the `String` length, but what you want to set is the amount of bytes this `String` takes up. – Ken Van Hoeylandt Jun 20 '16 at 12:48
  • 2
    Don't set the content length yourself. cf "streaming mode" in http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests which is only useful when you want to post large content that should not get buffered. For short parameters, let urlconnection do it. – zapl Jun 20 '16 at 13:07
  • @zapl, TY! solved. But how did you know that? and why in most Examples they used `Content-Length` in `URLConnection` ? and post an answer and let me accept it. – Yousha Aleayoub Jun 20 '16 at 13:19
  • 1
    `URLConnection` is terribly complicated, requires doing a lot of non-obvious things in the right order ([see](http://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection)) and the internet is full of [bad examples](https://en.wikipedia.org/wiki/Cargo_cult_programming) because of that. – zapl Jun 20 '16 at 13:42

0 Answers0