问题描述
我一直在试图为年龄来得到这个工作 - 但无论我做什么,我的HTTP *的取值的* POST总收益率
HttpUtils:javax.net.ssl.SSLException:不受信任的服务器证书
I've been trying for ages to get this to work - but no matter what I do, my HTTP*S* POST always yields HttpUtils: javax.net.ssl.SSLException: Not trusted server certificate
我基本上遵循了这一
- 我成功的抓住了从公证书(mycert.pem)
服务器。 - 我成功地创建使用充气城堡从证书密钥库
-
我在实现自定义的Apache HttpClient的失败。这是我的
code:
- I successfully grabbed the public certificate (mycert.pem) from theserver.
- I successfully created a keystore from the certificate using Bouncy Castle
I failed at implementing a custom Apache HttpClient. Here is mycode:
import android.content.Context;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.params.HttpParams;
import java.io.InputStream;
import java.security.KeyStore;
public class MyHttpClient extends DefaultHttpClient {
final Context context;
public MyHttpClient(Context context) {
this.context = context;
}
public MyHttpClient(Context context2, HttpParams myParams) {
super(myParams);
this.context= context2;
}
@Override protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}
private SSLSocketFactory newSslSocketFactory() {
try {
KeyStore trusted = KeyStore.getInstance("BKS");
InputStream in = context.getResources().openRawResource(R.raw.mystore);
try {
trusted.load(in, "password".toCharArray());
} finally {
in.close();
}
return new SSLSocketFactory(trusted);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
在我的HTTP请求的类,构建POST:
And in my HTTP Request class that constructs the POST:
public class HttpRequest {
MyHttpClient httpClient;
HttpContext localContext;
private String ret;
HttpResponse response = null;
HttpPost httpPost = null;
HttpGet httpGet = null;
public HttpRequest(Context context){
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
httpClient = new MyHttpClient(context, myParams);
localContext = new BasicHttpContext();
}
public String sendPost(String url, String data, String contentType) {
ret = null;
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
httpPost = new HttpPost(url);
response = null;
StringEntity tmp = null;
httpPost.setHeader("User-Agent", "SET YOUR USER AGENT STRING HERE");
httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
if (contentType != null) {
httpPost.setHeader("Content-Type", contentType);
} else {
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
}
try {
tmp = new StringEntity(data,"UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("Log", "HttpUtils : UnsupportedEncodingException : "+e);
}
httpPost.setEntity(tmp);
try {
response = httpClient.execute(httpPost,localContext);
if (response != null) {
ret = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
Log.e("Log", "HttpUtils: " + e);
}
return ret;
}
}
我的文章对非HTTPS网站的正常工作。任何帮助将大大AP preciated它。
My POST works fine for non-https websites. Any help would be greatly appreciated it.
推荐答案
看错误消息:
HttpUtils:javax.net.ssl.SSLException:不受信任的服务器证书
这意味着正是它说 - 服务器没有使用受信任的证书。我敢打赌,如果您尝试访问与Firefox或IE浏览器在同一台服务器,你会得到类似的错误。
This means exactly what it says -- the server is not using a trusted certificate. I bet you'll get similar errors if you try to access the same server with Firefox or IE.
这篇关于Android的HTTPS邮政 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!