HttpUrlConnection不与服务器联系

时间:2015-08-27 15:49:30

标签: android asp.net-web-api httpurlconnection

我正在尝试连接到服务器以创建新帐户。我正在将完整的URL捕获到字符串中以进行调试,然后将URL设置为该字符串并尝试打开该连接。当我将URL复制并粘贴到我的浏览器中时,我得到了回复并正确执行。但是,在我的应用程序中,我没有收到回复,甚至我的Toast通知都没有出现。我做错了吗?

private void postAPI(String userName, String password, String emailAddres, String firstName,
                     String lastName, String phone, String officeName, String address,
                     String city, String state, String zip, String salesRep) {
    final String tempAddress = ("https://www.exampleAddress.com/mobilenewacct.asp?u=" +
            userName + "&p=" + password + "&email=" + emailAddres + "&fname=" + firstName +
            "&lname=" + lastName + "&phone=" + phone + "&agoffice=" + officeName +
            "&agaddress=" + address + "&agcity=" + city + "&agstate=" + state +
            "&agzip=" + zip + "&repid=" + 1296).replace("@", "atsign");

    new AsyncTask<Void, Void, Boolean>(){
        @Override
        protected Boolean doInBackground(Void... params){
            try {
                url = new URL(tempAddress);

                urlConnection = (HttpURLConnection) url.openConnection();

                Log.v("Connection Open", "Open");

                urlConnection.connect();

                Log.v("Connection Connected", "Connected");

            } catch (Exception e) {
                //ERROR
                e.printStackTrace();
            } finally {
                try {
                    urlConnection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
        @Override
        public void onPostExecute(Boolean result){
            //Some message that indicates the connection was finished, or nothing.
        }
    }.execute();
}

Logcat输出

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:322)
W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:155)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
W/System.err﹕ at com.octitle.ryann.octmobile.NewUserActivity$2.doInBackground(NewUserActivity.java:133)
W/System.err﹕ at com.octitle.ryann.octmobile.NewUserActivity$2.doInBackground(NewUserActivity.java:123)
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
W/System.err﹕ Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
W/System.err﹕ at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:318)
W/System.err﹕ at com.android.org.conscrypt.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:219)
W/System.err﹕ at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:114)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:550)
W/System.err﹕ at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
W/System.err﹕ ... 16 more
W/System.err﹕ Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
W/System.err﹕ ... 22 more

1 个答案:

答案 0 :(得分:1)

在我尝试打开连接之前,我最终将此添加到我的代码中,直到我可以向设计服务器的开发人员询问证书是否是自签名的

SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] {
new X509TrustManager() {
     public void checkClientTrusted(X509Certificate[] chain, String authType) {}
     public void checkServerTrusted(X509Certificate[] chain, String authType) {}
     public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
     }
}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
     public boolean verify(String hostname, SSLSession session) {
          return true;
     }
});

在这里找到

HTTPS GET (SSL) with Android and self-signed server certificate