title: org.apache.http.client.ClientProtocolException
body: <p>I've made an Android application that uses a X509 certificate (that is in the folder res/raw/mykeystore.bks) to sign to remote server that respond on the 9006 port.</p>
  
  <p>the server ask me for a login (username, password).</p>
  
  <p><strong>when i make an HTTPGet i've the following exeption:
  org.apache.http.client.ClientProtocolException</strong></p>
  
  <p>Here is my implementation:</p>
  
  <p>The main Activity:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button b= (Button) findViewById(R.id.button1);
      b.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {               
               CredentialsProvider credProvider = new BasicCredentialsProvider();
                  credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                      new UsernamePasswordCredentials("rat#1", "rat"));
              HttpClient client = new MyHttpClient(getApplicationContext());
                 ((AbstractHttpClient) client).setCredentialsProvider(credProvider);
  
                 //final String url = "https://211.92.106.38:9006/KPIRest/testKpi/6";
                 final String url = "https://211.92.106.38/KPIRest/testKpi/6";
                 HttpGet httpGet = new HttpGet(url);
  
                 try {
                  HttpResponse response = client.execute(httpGet);
              } catch (ClientProtocolException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          }
      });
  </code></pre>
  
  <p>Custom Client Class:</p>
  
  <pre><code>public class MyHttpClient extends DefaultHttpClient {
  
  final Context context;
  
  public MyHttpClient(Context context) {
      this.context = context;
  }
  
  @Override
  protected ClientConnectionManager createClientConnectionManager() {
  
          KeyStore trustStore = null;
              trustStore = KeyStore.getInstance("BKS");
  
          InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
          try {
              // Initialize the keystore with the provided trusted certificates
              // Also provide the password of the keystore
              trustStore.load(in, "root01".toCharArray());
          } 
          } finally {
  
                  in.close();
  
          }
  
          SSLSocketFactory sf=null;
  
              sf = new MySSLSocketFactory(trustStore);
  
          sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  
          HttpParams params = new BasicHttpParams();
          HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
          HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  
          SchemeRegistry registry = new SchemeRegistry();
          registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
          registry.register(new Scheme("https", sf, 9006));
      return new SingleClientConnManager(params, registry);
  }
  }
  </code></pre>
  
  <p>My Customc SSLSoketFactory class:</p>
  
  <pre><code>public class MySSLSocketFactory extends SSLSocketFactory {
  SSLContext sslContext = SSLContext.getInstance("TLS");
  
  public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
      super(truststore);
  
      TrustManager tm = new X509TrustManager() {
          public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
          }
  
          public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
          }
  
          public X509Certificate[] getAcceptedIssuers() {
              return null;
          }
      };
  
      sslContext.init(null, new TrustManager[] { tm }, null);
  }
  
  @Override
  public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
      return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
  }
  
  @Override
  public Socket createSocket() throws IOException {
      return sslContext.getSocketFactory().createSocket();
  }
  }
  </code></pre>
  
  <p>what's wrong in my application? What causes that Exception?</p>
  
  <p>Thank you all!</p>
  
  <p>EDIT:</p>
  
  <p>I was looking better the exception:</p>
  
  <p>cause=
  org.apache.http.auth.MalformedChallengeException: Authentication challenge is empty.</p>
  
  <p>EDIT 2:</p>
  
  <p>I've tryed to use <a href="http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/">this</a> implementation with no difference, I've the same exception!</p>
  
  <p>EDIT 3: I've replaced </p>
  
  <pre><code> CredentialsProvider credProvider = new BasicCredentialsProvider();
                  credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                      new UsernamePasswordCredentials("rat#1", "rat"));
  </code></pre>
  
  <p>client).setCredentialsProvider(credProvider);</p>
  
  <p>with the base httpclient autentication, adding the header Authorization to the httpGet:</p>
  
  <pre><code>  httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.DEFAULT));
  </code></pre>
  
  <p>now the server send me this message:</p>
  
  <pre><code>HTTP/1.1 400 Bad Request 
  </code></pre>