最近发布了一个关于HttpClient
过度Https 的问题(在这里找到).我已经取得了一些进展,但我遇到了新的问题.和我的上一个问题一样,我似乎无法找到适合我的任何地方的例子.基本上,我希望我的客户端接受任何证书(因为我只指向一个服务器),但我一直在接受javax.net.ssl.SSLException: Not trusted server certificate exception.
所以这就是我所拥有的:
public void connect() throws A_WHOLE_BUNCH_OF_EXCEPTIONS {
HttpPost post = new HttpPost(new URI(PROD_URL));
post.setEntity(new StringEntity(BODY));
KeyStore trusted = KeyStore.getInstance("BKS");
trusted.load(null, "".toCharArray());
SSLSocketFactory sslf = new SSLSocketFactory(trusted);
sslf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme ("https", sslf, 443));
SingleClientConnManager cm = new SingleClientConnManager(post.getParams(),
schemeRegistry);
HttpClient client = new DefaultHttpClient(cm, post.getParams());
HttpResponse result = client.execute(post);
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
W/System.err( 901): javax.net.ssl.SSLException: Not trusted server certificate
W/System.err( 901): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:360)
W/System.err( 901): …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用loopj来进行异步 HTTP
请求.工作得很好,除非我尝试使用自签名证书访问https站点.我明白了
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate.
Run Code Online (Sandbox Code Playgroud)
我想默认的ssl选项可以覆盖使用setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
方法,但我不知道该怎么做,或者它可能根本不是正确的方法.
请建议我该如何解决这个问题?
我正在使用HttpClient进行HTTPS请求,到目前为止一直运行良好.升级到ICS后,一些用户报告连接3G连接时出现问题.
编辑:他们中的大多数似乎使用代理,我可以使用他们的代理在本地使用T-Mobile SIM重现.
日志具有此堆栈跟踪:
java.lang.IllegalStateException: Scheme 'http' not registered.
org.apache.http.conn.scheme.SchemeRegistry.getScheme(SchemeRegistry.java:80)
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:126)
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
Run Code Online (Sandbox Code Playgroud)
我们的端点仅限HTTPS,因此我们不会故意在SchemeRegistry中注册HTTP端点.我们没有任何地方(AFAIK)重定向到HTTP.
以下是为HTTPS客户端设置HttpClient的代码:
DefaultHttpClient ret = null;
// sets up parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "utf-8");
params.setBooleanParameter("http.protocol.expect-continue", false);
HttpConnectionParams.setConnectionTimeout(params, DEFAULT_CONN_TIMEOUT_MSEC);
HttpConnectionParams.setSoTimeout(params, timeoutMsec);
HttpConnectionParams.setStaleCheckingEnabled(params, true);
SchemeRegistry registry = new SchemeRegistry();
final SocketFactory sslSocketFactory = getPreferredSSLSocketFactory();
registry.register(new Scheme("https", sslSocketFactory, 443));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
ret = new DefaultHttpClient(manager, params);
// for preemptive authentication
// http://dlinsin.blogspot.com/2009/08/http-basic-authentication-with-android.html
ret.addRequestInterceptor(preemptiveAuth, 0);
ret.setCookieStore(communalCookieJar);
SimpleCredentialsProvider credProvider …
Run Code Online (Sandbox Code Playgroud) 我试图从我的Android/Java源代码访问Basecamp API ....
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class BCActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DefaultHttpClient httpClient = new DefaultHttpClient();
//final String url = "https://encrypted.google.com/webhp?hl=en"; //This url works
final String url = "https://username:password@projectsource.basecamphq.com/people.xml"; //This don't
HttpGet http = new HttpGet(url);
http.addHeader("Accept", "application/xml");
http.addHeader("Content-Type", "application/xml");
try {
// HttpResponse response = …
Run Code Online (Sandbox Code Playgroud) 我正面临着一个javax.net.ssl.SSLPeerUnverifiedException:在Android模拟器中使用google place api时没有对等证书异常.我是我的网址:
https://maps.googleapis.com/maps/api/place/search/json?&location=28.632808,77.218276&radius=1000&sensor=false&key=AIzaSyC3y3fOMTqTkCjNNETQTCKustG7BRk_eVc
Run Code Online (Sandbox Code Playgroud)
当我简单地将上面的URL粘贴到浏览器地址栏中时,它简单地给出了Json字符串.所以,我认为不需要任何证书身份验证.
经过这么多谷歌搜索和投入2-3天后,我使用了org.apache.http.conn.ssl.SSLSocketFactory包的SSLSocketFacory类来避免对等证书错误.
下面是我的代码:
public static HttpClient wrapClient(HttpClient base) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
X509HostnameVerifier verifier = new X509HostnameVerifier() {
public void verify(String string, SSLSocket ssls) throws IOException {
}
public void verify(String string, X509Certificate xc) throws SSLException {
}
public …
Run Code Online (Sandbox Code Playgroud) 我有以下错误Hostname domain.com not verified:
不是"javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated"
我以前能够连接到不同的主机,在这个主机我遇到问题,如何解决它
javax.net.ssl.SSLPeerUnverifiedException: Hostname domain.com not verified:
certificate: sha1//WQM9QbrKs6DCFa9qN/EIw1ywBw=
DN: CN=*.ipage.com,OU=Domain Control Validated - RapidSSL(R),OU=See www.rapidssl.com/resources/cps (c)14,OU=GT29505539
subjectAltNames: [*.ipage.com, ipage.com]
at com.squareup.okhttp.Connection.connectTls(Connection.java:244)
at com.squareup.okhttp.Connection.connectSocket(Connection.java:199)
at com.squareup.okhttp.Connection.connect(Connection.java:172)
at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:367)
at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
at com.squareup.okhttp.Call.getResponse(Call.java:267)
Run Code Online (Sandbox Code Playgroud)
这是我的代码
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try{
RequestBody formBody = new …
Run Code Online (Sandbox Code Playgroud) android ×5
java ×2
ssl ×2
basecamp ×1
certificate ×1
httpclient ×1
https ×1
loopj ×1
okhttp ×1