org.apache.http.client.ClientProtocolException [英] org.apache.http.client.ClientProtocolException
问题描述
我做了一个使用X509证书(即在文件夹RES /生/ mykeystore.bks)签署到了9006端口上响应远程服务器上的Android应用程序。
服务器要求我登录(用户名,密码)。
当我做一个HTTPGET我下面exeption: org.apache.http.client.ClientProtocolException
下面是我的实现:
主要活动:
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮B =(按钮)findViewById(R.id.button1);
b.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
CredentialsProvider credProvider =新BasicCredentialsProvider();
credProvider.setCredentials(新AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT)
新UsernamePasswordCredentials(鼠#1,鼠));
HttpClient的客户端=新MyHttpClient(getApplicationContext());
((AbstractHttpClient)客户端).setCredentialsProvider(credProvider);
//最后的字符串URL =https://211.92.106.38:9006/KPIRest/testKpi/6;
最终的字符串URL =https://211.92.106.38/KPIRest/testKpi/6;
HTTPGET HTTPGET =新HTTPGET(URL);
尝试 {
HTT presponse响应= client.execute(HTTPGET);
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
});
自定义客户端类:
公共类MyHttpClient扩展DefaultHttpClient {
最后上下文的背景下;
公共MyHttpClient(上下文的背景下){
this.context =背景;
}
@覆盖
保护ClientConnectionManager createClientConnectionManager(){
密钥库的trustStore = NULL;
的trustStore = KeyStore.getInstance(BKS);
InputStream的时间= context.getResources()openRawResource(R.raw.mykeystore)。
尝试 {
//初始化与提供受信任的证书的密钥库
//还提供密钥库的密码
trustStore.load(在root01.toCharArray());
}
} 最后 {
附寄();
}
SSLSocketFactory的SF = NULL;
SF =新MySSLSocketFactory(的trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
的HttpParams PARAMS =新BasicHttpParams();
HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(参数,可以HTTP.UTF_8);
SchemeRegistry注册表=新SchemeRegistry();
registry.register(新计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
registry.register(新计划(https开头,SF,9006));
返回新SingleClientConnManager(参数,可以登记);
}
}
我的Customc SSLSoketFactory类:
公共类MySSLSocketFactory扩展的SSLSocketFactory {
的SSL连接的SSL连接= SSLContext.getInstance(TLS);
公共MySSLSocketFactory(密钥库信任库)抛出抛出:NoSuchAlgorithmException,KeyManagementException,KeyStoreException,UnrecoverableKeyException {
超(信任库);
的TrustManager TM =新X509TrustManager(){
公共无效checkClientTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
}
公共无效checkServerTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
}
公共x509证书[] getAcceptedIssuers(){
返回null;
}
};
sslContext.init(空,新的TrustManager [] {} TM,NULL);
}
@覆盖
公共插座中的createSocket(Socket套接字,字符串主机,INT端口,布尔自动关闭)抛出IOException异常,UnknownHostException异常{
返回sslContext.getSocketFactory()中的createSocket(插座,主机,端口自动关闭)。
}
@覆盖
公共插座中的createSocket()抛出IOException异常{
返回sslContext.getSocketFactory()中的createSocket()。
}
}
什么是错在我的应用程序?是什么原因导致的异常?
感谢大家!
编辑:
我一直在寻找更好的异常:
原因= org.apache.http.auth.MalformedChallengeException:认证挑战是空的。
编辑2:
我tryed使用这个的实施没有区别,我有相同的异常!
编辑3:我把它换成
CredentialsProvider credProvider =新BasicCredentialsProvider();
credProvider.setCredentials(新AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT)
新UsernamePasswordCredentials(鼠#1,鼠));
客户端).setCredentialsProvider(credProvider);
与基地HttpClient的autentication,添加标题授权给HTTPGET:
httpGet.addHeader(授权,基本+ Base64.en codeToString(鼠#1:鼠.getBytes(),Base64.DEFAULT));
现在的服务器发送此消息:
HTTP / 1.1 400错误的请求
问题是授权头。
我们必须使用:
httpGet.addHeader(授权,基本+ Base64.en codeToString(鼠#1:鼠.getBytes(),Base64.NO_WRAP));
相反的:
httpGet.addHeader(授权,基本+ Base64.en codeToString(鼠#1:鼠.getBytes(),Base64.DEFAULT));
因为默认参数添加CR行终止字符串的结尾,它的uncorrect如果你会用它的头。
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.
the server ask me for a login (username, password).
when i make an HTTPGet i've the following exeption: org.apache.http.client.ClientProtocolException
Here is my implementation:
The main Activity:
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();
}
}
});
Custom Client Class:
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);
}
}
My Customc SSLSoketFactory class:
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();
}
}
what's wrong in my application? What causes that Exception?
Thank you all!
EDIT:
I was looking better the exception:
cause= org.apache.http.auth.MalformedChallengeException: Authentication challenge is empty.
EDIT 2:
I've tryed to use this implementation with no difference, I've the same exception!
EDIT 3: I've replaced
CredentialsProvider credProvider = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("rat#1", "rat"));
client).setCredentialsProvider(credProvider);
with the base httpclient autentication, adding the header Authorization to the httpGet:
httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.DEFAULT));
now the server send me this message:
HTTP/1.1 400 Bad Request
the problem was the Authorization header.
We have to use:
httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.NO_WRAP));
Instead of:
httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("rat#1:rat".getBytes(),Base64.DEFAULT));
because the DEFAULT parameter add "CR" line terminator at the end of string and it's uncorrect if you'll use it that header.
这篇关于org.apache.http.client.ClientProtocolException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!