gpt4 book ai didi

Android HTTPS 发布 - 不工作

转载 作者:行者123 更新时间:2023-11-29 22:17:06 27 4
gpt4 key购买 nike

多年来我一直在努力使它正常工作 - 但无论我做什么,我的 HTTP*S* POST 总是会产生 HttpUtils:javax.net.ssl.SSLException:不可信的服务器证书

基本上我是按照这个tutorial

  • 我成功地从服务器。
  • 我使用 Bouncy CaSTLe 从证书成功创建了一个 keystore
  • 我未能实现自定义 Apache HttpClient。这是我的代码:

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

    在我构造 POST 的 HTTP 请求类中:

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

我的 POST 适用于非 https 网站。任何帮助将不胜感激。

最佳答案

查看错误信息:

HttpUtils: javax.net.ssl.SSLException: Not trusted server certificate

这就是它所说的意思——服务器没有使用受信任的证书。我敢打赌,如果您尝试使用 Firefox 或 IE 访问同一台服务器,您会遇到类似的错误。

关于Android HTTPS 发布 - 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8222406/

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