title: Program runs on PC, throws ProtocolException: Too many redirects: 21 on Android
body: <p>I've already found a few questions about this on SO (<a href="https://stackoverflow.com/questions/23499009/java-net-protocolexception-too-many-redirects-21-android-app">here</a>'s one of them), but I cannot figure out why the exception is raised when running the code on Android but not when using the JRE on my PC (Windows). I use exactly the same code and libraries.</p>
  
  <p>Here's the code that throws the exception (some variables are declared prior to this scope):</p>
  
  <pre><code>URL url = new URL(urlString);
  conn = (HttpsURLConnection) url.openConnection();
  
  conn.setRequestMethod("GET");
  conn.setUseCaches(false);
  conn.setRequestProperty("User-Agent", userAgent);
  conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  if (cookies != null) {
      for (String cookie : this.cookies) {
          conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
      }
  }
  int responseCode = conn.getResponseCode();
  Log.d(TAG, "Response code: " + responseCode);
  
  BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  String inputLine;
  StringBuilder response = new StringBuilder();
  
  while ((inputLine = in.readLine()) != null) {
      response.append(inputLine);
  }
  in.close();
  
  setCookies(conn.getHeaderFields().get("Set-Cookie"));
  
  return response.toString();
  </code></pre>
  
  <p>The exception is thrown on <code>conn.getResponseCode</code> (if removed, it's also thrown on <code>conn.getInputStream</code>). Here's the stack trace:</p>
  
  <pre><code>28939-28998/com.example.www W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:454)
  28939-28998/com.example.www W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:348)
  28939-28998/com.example.www W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:520)
  28939-28998/com.example.www W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
  28939-28998/com.example.www W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
  </code></pre>
  
  <p>Since I'm not sending the request to my server but simulating a browser, I cannot make changes in the server code.</p>
  
  <p>Any idea why this might happen on Android while it works smoothly on my PC? I'm considering trying a different library for HTTP requests (perhaps <a href="https://github.com/turbomanage/basic-http-client" rel="nofollow noreferrer">Basic HTTP Client</a>), but I'm afraid it's gonna be a dull work with the same results. I also considered increasing the redirects limitation but that seems like a bad workaround.</p>
  
  <p>Thanks in advance!</p>