Android通过HTTPS使用Ksoap2请求

时间:2018-07-10 07:17:14

标签: java android soap https ksoap2

我在与我的应用android一起使用soap请求时遇到问题。 我使用Ksoap2库。这是我的代码:

public class WebServiceCall {

private static final String TAG = WebServiceCall.class.getSimpleName();

public static String callWSThreadSoapPrimitive(String strURL, String strSoapAction, SoapObject request) {


    try {
        StringBuffer result;
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE ht = new HttpTransportSE(strURL);
        ht.debug = true;
        ht.call(strSoapAction, envelope);
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

        result = new StringBuffer(response.toString());
        Log.i(TAG, "result: " + result.toString());
        return result.toString();

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


}

}

public class GetArticleTask extends AsyncTask<String, Void, String> {

private MainActivity activity;
private String soapAction;
private String methodName;
private String paramsName;

private final static String TAG = GetArticleTask.class.getSimpleName();

public GetArticleTask(MainActivity activity, String soapAction, String methodName,
                              String paramsName) {
    this.activity = activity;
    this.methodName = methodName;
    this.soapAction = soapAction;
    this.paramsName = paramsName;
}

@Override
protected String doInBackground(String... params) {
    //create a new soap request object
    SoapObject request = new SoapObject(ConstantString.NAME_SPACE, methodName);
    //add properties for soap object
    request.addProperty(paramsName, params[0]);

    //request to server and get Soap Primitive response
    return WebServiceCall.callWSThreadSoapPrimitive(ConstantString.URL, soapAction, request);
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    if (result == null) {
        Log.i(TAG, "cannot get result");
    } else {
        //invoke call back method of Activity
        activity.callBackDataFromAsyncTask(result);
    }
}

}

public class ConstantString {

public final static String SOAP_ACTION = "https://xxx.xxx.xxx.xxx:1443/orawsv/USER/WSS_MWEB_CLI/";
public final static String NAME_SPACE = "https://xxx.xxx.xxx.xxx:1443/orawsv/USER/WSS_MWEB_CLI/";

public final static String URL ="https://xxx.xxx.xxx.xxx:1443/orawsv/USER/WSS_USER_CLI/GETARTICLE";

public final static String GET_ARTICLE_METHOD_NAME = "GETARTICLE";
public final static String GET_ARTICLE_SOAP_ACTION = SOAP_ACTION + GET_ARTICLE_METHOD_NAME;

}

public class MainActivity extends Activity {

private TextView textConverted;
private View btnGetArticle;
private EditText input;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnGetArticle = (View) findViewById(R.id.btn);
    textConverted = (TextView) findViewById(R.id.txt_converted);
    input = (EditText) findViewById(R.id.txt_temp);

    // set event listeners
    btnGetArticle.setOnClickListener(onFtoCClick());
}

//change Fahrenheit to Celsius degree
private OnClickListener onFtoCClick() {
    return new OnClickListener() {

        @Override
        public void onClick(View v) {
            invokeAsyncTask("Article", ConstantString.GET_ARTICLE_SOAP_ACTION,
                    ConstantString.GET_ARTICLE_METHOD_NAME);
        }
    };
}

//create and execute asynctask to get response from W3school server
private void invokeAsyncTask(String convertParams, String soapAction, String methodName) {
    new GetArticleTask(this, soapAction, methodName, convertParams).execute(input.getText()
            .toString().trim());
}

//call back data from background thread and set to views
public void callBackDataFromAsyncTask(String result) {

    textConverted.setText(result);


}

}

当我按一下按钮进行测试时,会收到以下错误消息:

  

07-10 08:52:17.852 702-754 / com.ablinfo.mweb W / System.err:javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。   07-10 08:52:17.857 702-754 / com.ablinfo.mweb W / System.err:at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:328)           在com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:103)           在com.android.okhttp.Connection.connect(Connection.java:143)           在com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)           在com.android.okhttp.OkHttpClient $ 1.connectAndSetOwner(OkHttpClient.java:128)           在com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:342)           在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:331)   07-10 08:52:17.858 702-754 / com.ablinfo.mweb W / System.err:at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)           在com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)           在com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)           在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)           在com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)           在com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)           在org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:130)           在org.ksoap2.transport.HttpTransportSE.sendData(HttpTransportSE.java:292)           在org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:184)           在org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)           在org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)           在com.ablinfo.mweb.service.WebServiceCall.callWSThreadSoapPrimitive(WebServiceCall.java:29)           在com.ablinfo.mweb.service.GetArticleTask.doInBackground(GetArticleTask.java:36)           在com.ablinfo.mweb.service.GetArticleTask.doInBackground(GetArticleTask.java:11)           在android.os.AsyncTask $ 2.call(AsyncTask.java:295)           在java.util.concurrent.FutureTask.run(FutureTask.java:237)           在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)           在java.lang.Thread.run(Thread.java:818)           禁止:javax.net.ssl.SSLHandshakeException:握手失败

你能帮我吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

我已解决此问题,以修改我的WebServiceCall:

print(sum(len(x) for x in Names))
# 6