本文介绍了Android通过HTTPS使用Ksoap2请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I have a problem to use a soap request with my app android.I use Ksoap2 library. this is my code:

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


}

}

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

when I clic on a buton to test it, I have this error message:

你能帮我吗?

谢谢.

推荐答案

我解决了修改我的WebServiceCall的问题:

I resolve this problem to modify my WebServiceCall:

public class WebServiceCall {

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

private static TrustManager[] trustManagers;

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);
        // certificat SSL
        allowAllSSL();
        HttpTransportSE ht = new HttpTransportSE(strURL);

        List<HeaderProperty> llstHeadersProperty = new ArrayList<>();
        llstHeadersProperty.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("user:password".getBytes())));

        ht.debug = true;
        ht.call(strSoapAction, envelope,llstHeadersProperty);
        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 static class _FakeX509TrustManager implements
        javax.net.ssl.X509TrustManager {
    private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};

    public void checkClientTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
    }

    public boolean isClientTrusted(X509Certificate[] chain) {
        return (true);
    }

    public boolean isServerTrusted(X509Certificate[] chain) {
        return (true);
    }



    public X509Certificate[] getAcceptedIssuers() {
        return (_AcceptedIssuers);
    }
}

public static void allowAllSSL() {

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

    javax.net.ssl.SSLContext context = null;

    if (trustManagers == null) {
        trustManagers = new javax.net.ssl.TrustManager[] { new _FakeX509TrustManager() };
    }

    try {
        context = javax.net.ssl.SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        Log.e("allowAllSSL", e.toString());
    } catch (KeyManagementException e) {
        Log.e("allowAllSSL", e.toString());
    }
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context
            .getSocketFactory());
}

这篇关于Android通过HTTPS使用Ksoap2请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:02