मैंने एक एंड्रॉइड एप्लिकेशन बनाया है जो 9006 पोर्ट पर प्रतिक्रिया देने वाले दूरस्थ सर्वर पर हस्ताक्षर करने के लिए X509 प्रमाणपत्र (जो फ़ोल्डर res/raw/mykeystore.bks में है) का उपयोग करता है।org.apache.http.client.ClientProtocolException
सर्वर मुझे लॉगिन (उपयोगकर्ता नाम, पासवर्ड) के लिए पूछता है।
जब मैं बनाने के एक HTTPGet मैं है निम्नलिखित अपवाद:
मुख्य गतिविधि::
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: प्रमाणीकरण चुनौती खाली है।
संपादित करें 2:
मैं कोई अंतर के साथ this कार्यान्वयन का उपयोग करने के लिए tryed है, मैं एक ही अपवाद है!
संपादित करें 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
शानदार @ मार्को .... मैं पूरे दिन इस त्रुटि के साथ काम कर रहा था !! बहुत बहुत धन्यवाद। –