title: HttpURLConnection header max length in Android
body: <p>I have the following code</p>
  
  <h1>Update:There problem exists only in Android apps,it works fine in a java application</h1>
  
  <p><em>Java (Not working for large header value)</em></p>
  
  <pre><code>URL url = new URL("MyUrl"); //&lt;===This could even be http://www.google.com  
  HttpURLConnection request = (HttpURLConnection) url.openConnection();
  
  String userCredentials = "username:AAAAABBBBBBBBCCCCSSSSSSSSSDDDDDDDDDDBE4B97CC9E81DB";
  String basicAuth = "Basic " +  android.util.Base64.encodeToString(
              userCredentials.getBytes(), android.util.Base64.DEFAULT);
  request.setRequestProperty ("Authorization", basicAuth);
  
  request.connect();
  InputStream stream = (InputStream) request.getContent();
  </code></pre>
  
  <p>I get a ClassNotFoundException when I call getContent()</p>
  
  <pre><code>java.lang.ClassNotFoundException: Didn't find class "org.apache.harmony.awt.www.content.text.html__charset_us_ascii" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
  </code></pre>
  
  <p>in ClassLoader.class Line 514</p>
  
  <pre><code> protected Class&lt;?&gt; loadClass(String className, boolean resolve) throws ClassNotFoundException {
      Class&lt;?&gt; clazz = findLoadedClass(className);
  
  
      if (clazz == null) {
          ClassNotFoundException suppressed = null;
          try {
              clazz = parent.loadClass(className, false);
          } catch (ClassNotFoundException e) {
              suppressed = e;
          }
  
          if (clazz == null) {
              try {
                  clazz = findClass(className);
              } catch (ClassNotFoundException e) {
                  e.addSuppressed(suppressed);
                 //className=org.apache.harmony.awt.www.content.text.html__charset_us_ascii
                  throw e;  //&lt;===The code crashes here
  
              }
          }
      }
  
      return clazz;
  }
  </code></pre>
  
  <p>The problem is the size of the Authorization header,even if it is not that big (88 characters)<br>
  If I change the value of the header to something smaller it works.<br>
  <strong>Can I change somehow the max size of the header?</strong></p>
  
  <p>I have tried it from other platforms (Windows ,Windows Phone ,Web  ) and it works fine,the problem is not on my server.    </p>
  
  <p><strong>jQuery (Works always)</strong> </p>
  
  <pre><code>var usernameAndPasword=["username","AAAAABBBBBBBBCCCCSSSSSSSSSDDDDDDDDDDBE4B97CC9E81DB"].join(":");
  var authorizationHeaderValue="Basic "+   window.btoa(usernameAndPasword);
  
  $.ajax({
      url : "http://...",
      type: "GET",
      beforeSend: function (request) {    request.setRequestHeader("Authorization", authorizationHeaderValue); },
      success: function(data, textStatus, jqXHR)  {   alert(data +  "\n" + textStatus ); },
      error: function (jqXHR, textStatus, errorThrown){ alert(jqXHR.responseText +  " \n " + textStatus + " \n"+errorThrown); }
    }); 
  </code></pre>
  
  <p><strong>C# (Works always)</strong></p>
  
  <pre><code> HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUristring);
   request.Method = "GET";
   request.Headers["Accept-Encoding"] = "gzip, deflate";
  
   var bytes = Encoding.UTF8.GetBytes("username" + ":" + "AAAAABBBBBBBBCCCCSSSSSSSSSDDDDDDDDDDBE4B97CC9E81DB" );
   var base64 = Convert.ToBase64String(bytes);
   request.Headers["Authorization"] = "Basic " + base64;
  
   HttpWebResponse webResponse = (HttpWebResponse)request.GetResponseAsync().Result;
  </code></pre>