gpt4 book ai didi

java - org.apache.http.client.ClientProtocolException异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:51 27 4
gpt4 key购买 nike

我制作了一个 Android 应用程序,它使用 X509 证书(位于文件夹 res/raw/mykeystore.bks 中)来登录响应 9006 端口的远程服务器。

服务器要求我登录(用户名、密码)。

当我创建 HTTPGet 时出现以下异常:org.apache.http.client.ClientProtocolException

这是我的实现:

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

}
});

自定义客户端类:

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

我的申请有什么问题?是什么导致该异常?

谢谢大家!

编辑:

我看起来更好的异常(exception):

原因=org.apache.http.auth.MalformedChallengeException:身份验证质询为空。

编辑 2:

我试过使用 this实现没有区别,我有同样的异常(exception)!

编辑 3:我已经替换了

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

客户端).setCredentialsProvider(credProvider);

使用基本的 httpclient 验证,将 header 授权添加到 httpGet:

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

现在服务器向我发送此消息:

HTTP/1.1 400 Bad Request 

最佳答案

问题是授权 header 。

我们必须使用:

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

因为 DEFAULT 参数在字符串末尾添加了“CR”行终止符,如果您在该 header 中使用它是不正确的。

关于java - org.apache.http.client.ClientProtocolException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13494148/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com