2012-11-21 3 views
8

X509証明書(これはres/raw/mykeystore.bksというフォルダにあります)を使用して9006ポートで応答するリモートサーバに署名するAndroidアプリケーションを作成しました。org.apache.http.client.ClientProtocolException

サーバーはログイン(ユーザー名、パスワード)を求めます。

私が作るときHTTPGET I次exeptionました:

主なアクティビティー:

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(); 
      } 

     } 
    }); 
org.apache.http.client.ClientProtocolExceptionここ

は私の実装です

カスタムクライアントクラス:

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); 
} 
} 

マイCustomc SSLSoketFactoryクラス:

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(); 
} 
} 

私のアプリケーションで間違っているのでしょうか?その例外の原因は何ですか?

ありがとうございました!

編集:私は例外良く見ていた

原因を= org.apache.http.auth.MalformedChallengeException:認証チャレンジは空です。

EDIT 2:

ノー差がthis実装を使用するようにしようと試みてきたが、私は同じ例外をしました!

EDIT 3:私は

CredentialsProvider credProvider = new BasicCredentialsProvider(); 
       credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
        new UsernamePasswordCredentials("rat#1", "rat")); 

クライアントを交換しました).setCredentialsProvider(credProvider)。ベースのHTTPClient autenticationと

、HTTPGETにヘッダ認証を追加:

httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.DEFAULT)); 

今サーバーが私にこのメッセージを送信します

HTTP/1.1 400 Bad Request 

答えて

10

問題はAuthorizationヘッダーでした。

我々が使用する必要があります:

httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.NO_WRAP)); 

の代わりに:

httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.DEFAULT)); 

をデフォルトのパラメータは、文字列の末尾に「CR」行のターミネータを追加し、あなたがそれを使用しますならば、それは正しくなだからそのヘッダー。

+0

素晴らしい@Marco ....私は一日中このERRORでうんざりしていました!!ありがとうございました。 –

関連する問題