在Android中使用HttpURLConnection将编码的XML发布到URL时发生IOException

时间:2015-12-18 07:10:50

标签: android xml post utf-8 httpurlconnection

我在尝试使用Android中的HttpURLConnection将复杂编码的XML发布到网址时收到IOException。代码如下:

                try {
                URL url;
                url = new URL("....mobile.xml");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                // Preparing the connection
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("charset", "utf-8");
                conn.setUseCaches(false);
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setInstanceFollowRedirects(false);                                     

                // Sending request
                String body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><mobile>.....</mobile>"; // XML data

                OutputStream outStr = conn.getOutputStream();
                BufferedWriter buffWriter = new BufferedWriter(new OutputStreamWriter(outStr));
                buffWriter.write(body); // Write POST query string (if any needed)
                buffWriter.flush();                 
                buffWriter.close();
                outStr.close();

                // Receiving response
                int responseCode = conn.getResponseCode();

                if(responseCode == 200){
                    InputStream inpStr = conn.getInputStream();
                    BufferedReader buffRead = new BufferedReader(new InputStreamReader(inpStr));
                    String line;
                    StringBuffer response = new StringBuffer(); 
                    while((line = buffRead.readLine()) != null) {
                        response.append(line);
                        response.append('\r');
                    }
                    buffRead.close();
                    return response.toString();
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SocketTimeoutException e){
                e.printStackTrace();
            } catch (ConnectTimeoutException e){
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e){
                e.printStackTrace();
            }

            return "";

我不确定我哪里出错了。我忽略了什么吗?有什么东西可以添加或丢弃。我需要XML是UTF-8编码的。响应也是UTF-8编码的XML。

任何帮助将不胜感激。

这是stacktrace:

12-18 12:57:45.629: W/System.err(19317): javax.net.ssl.SSLHandshakeException: Handshake failed
12-18 12:57:45.632: W/System.err(19317):    at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:390)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.Connection.connect(Connection.java:155)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
12-18 12:57:45.632: W/System.err(19317):    at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
12-18 12:57:45.633: W/System.err(19317):    at com.spinnpress.profile.AccountFragment$TelrPostDataAsyncTask.doInBackground(AccountFragment.java:957)
12-18 12:57:45.634: W/System.err(19317):    at com.spinnpress.profile.AccountFragment$TelrPostDataAsyncTask.doInBackground(AccountFragment.java:1)
12-18 12:57:45.634: W/System.err(19317):    at android.os.AsyncTask$2.call(AsyncTask.java:292)
12-18 12:57:45.634: W/System.err(19317):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-18 12:57:45.634: W/System.err(19317):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-18 12:57:45.634: W/System.err(19317):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-18 12:57:45.635: W/System.err(19317):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-18 12:57:45.635: W/System.err(19317):    at java.lang.Thread.run(Thread.java:818)
12-18 12:57:45.636: W/System.err(19317): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb83d40b8: Failure in SSL library, usually a protocol error
12-18 12:57:45.636: W/System.err(19317): error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:770 0xadece005:0x00000000)
12-18 12:57:45.637: W/System.err(19317):    at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
12-18 12:57:45.637: W/System.err(19317):    at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
12-18 12:57:45.637: W/System.err(19317):    ... 17 more

0 个答案:

没有答案