使用KSOAP2

时间:2017-08-18 07:00:36

标签: android web-services ssl soap

我正在

  

javax.net.ssl.SSLHandshakeException:连接由对等方关闭

在Android 7.0 Nougat中使用KSOAP2(ksoap2_android_2.6.4)调用SOAP webservice时。我已经审阅了以下所有问题

“Connection closed by peer” error occurs in Android 7.0 Nougat while connecting to SHA256 CA installed Windows 2003 Server SP2 through HTTPS

Android 7.0 : 'javax.net.ssl.SSLHandshakeException: Connection closed by peer

以下是我调用webservice usiong KSOAP的代码

public class ValidateUserAsyncTask extends AsyncTask<String, Integer, Void> {
    ProgressDialog dialog = new ProgressDialog(LoginActivity.this);
    String tempAction = "";

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Validating User...");
        dialog.setCancelable(true);
        dialog.show();
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... strings) {

        try {
            if (SplashScreenActivity.checkNetworkConnection(getApplicationContext()) == true) {
                Log.d("", "host is reachable");
                callingValidateUser(strings[0], strings[1], strings[2], strings[3], strings[4]);

            } else {
                fault = true;
                strFault = "Please Check your Internet Connection!";
            }

        } catch (Exception e) {
            Log.d("Connection error", "Connection Error in call service");
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        try {

            dialog.dismiss();
            if (SplashScreenActivity.checkNetworkConnection(getApplicationContext()) == true) {

                ParsingForValidateUser(envelope_Actions);
            }
            if (fault == true) {
                if (strFault.contains("User Name/Password is not valid")) {
                    editor = setPref.edit();
                    editor.putBoolean("flagValidatedUser", false);
                    editor.commit();
                }
                if(strFault.contains("Contact to Hr Or Submit Your Mobile Number"))
                {
                    fault = false;
                    strFault = "";
                    Intent i = new Intent(getApplication(), NoMobileRegisterActivity.class);
                    startActivity(i);

                }
                else
                {
                    Toast.makeText(getApplicationContext(), "" + strFault, Toast.LENGTH_LONG).show();
                    fault = false;
                    strFault = "";
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void callingValidateUser(String userName, String password, String nameSpace, String methodName, String url) {

        androidHttpTransport = new HttpTransportSE(url, 60000);
        SoapObject request = new SoapObject(nameSpace, methodName);

        try {
            password = AESencrp.encrypt(password);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        request.addProperty("UserName", userName);
        request.addProperty("Password", password);

        Log.d("UserName", "" + userName);
        Log.d("password", "" + password);
        Log.d("nameSpace", "" + nameSpace);
        Log.d("methodName", "" + methodName);
        Log.d("url", "" + url);

        Element[] header = new Element[1];
        header[0] = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
        header[0].setAttribute(null, "mustUnderstand", "1");

        Element usernametoken = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken");
        usernametoken.setAttribute(null, "Id", "UsernameToken-1");
        header[0].addChild(Node.ELEMENT, usernametoken);

        Element username = new Element().createElement(null, "n0:Username");
        username.addChild(Node.IGNORABLE_WHITESPACE, userName);
        usernametoken.addChild(Node.ELEMENT, username);

        Element pass = new Element().createElement(null, "n0:Password");
        pass.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        pass.addChild(Node.TEXT, password);
        usernametoken.addChild(Node.ELEMENT, pass);

        envelope_Actions.headerOut = header;
        envelope_Actions.dotNet = true;
        envelope_Actions.bodyOut = request;

        envelope_Actions.setOutputSoapObject(request);
        try {
            androidHttpTransport.debug = true;
            androidHttpTransport.call("process", envelope_Actions); // Calling
        } catch (SoapFault e) {
            Log.d("", "SoapFault ERROR in call worklist type");
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d("", "XmlPullParserException ERROR in call worklist type");
            e.printStackTrace();
        } catch (UnknownHostException e) {
            Log.d("", "UnknownHostException ERROR in call worklist type");
            e.printStackTrace();

        } catch (MalformedURLException e) {
            Log.d("", "MalformedURLException ERROR in call worklist type");
            e.printStackTrace();

        } catch (SocketTimeoutException e) {
            fault = true;
            strFault = "Request Time Out";
            Log.d("", "SocketTimeoutException ERROR in call worklist type");
            e.printStackTrace();

        } catch (IOException e) {
            Log.d("", "IOException ERROR in call worklist type");
            e.printStackTrace();
        }

    }

    public void ParsingForValidateUser(SoapSerializationEnvelope envelope) {

        SoapObject result;
        db = dbHelper.getWritableDatabase();
        try {
            result = (SoapObject) envelope.getResponse();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            result = (SoapObject) envelope.bodyIn;
            String res = result.getPropertyAsString(0).toString();
        } catch (SoapFault e) {
            e.printStackTrace();
        } 

    }

}

我已经问过处理服务器配置的人。检查TLS是否启用?等待他的回复。 我也创建了 network_security_config.xml ,如下所示

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
       <base-config>
           <trust-anchors>
               <certificates src="system"/>
           </trust-anchors>
       </base-config>
</network-security-config>

并在 Menifest.xml

中声明
android:networkSecurityConfig="@xml/network_security_config"

我还尝试将证书放在原始文件夹中的PEM formate中,但无法弄清楚这个问题。

请指导我在哪里犯错误?

0 个答案:

没有答案