当前位置: 首页 > 知识库问答 >
问题:

Android将Ksoap2请求与HTTPS一起使用

薄龙光
2023-03-14

我有一个问题,在我的应用程序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);


}

共有1个答案

陶成化
2023-03-14

我通过修改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());
}
 类似资料:
  • 问题内容: 有没有人能够通过https使用kso​​ap2 android连接到Soap服务器? 我不断收到“主机名<###>未被验证”的错误 我正在做 显然回头看看不是kandroid的其他ksoap,您可以通过https连接到我们的电话,但我找不到在android版本中做到这一点的方法。 是否有人找到或知道要使用的通话方式? 谢谢你的帮助 问题答案: android ksoap2 2.5.2已

  • 问题内容: 我有一台使用HTTPS且使用Firefox的Grizzly制造的REST服务器。这是代码: 现在,我尝试用Java达到它: 得到: 从我在网上找到的示例来看,似乎需要设置一个Truststore,然后再设置某种TrustManager。对于我这个简单的小项目,这似乎需要大量代码和设置工作。有没有更简单的方法可以说..我信任此证书并指向.cert文件? 问题答案: 当您说“是否有一种更简

  • 背景 我在我的android应用程序中使用清洁架构。我的域和数据层高度基于RxJava(RxAndroid)。我还在使用Parse(BaaS)。 问题 我知道Android Parse SDK在幕后做了很多工作,比如缓存和加密数据。将其同步调用与RxJava的调度器一起使用真的很稳定吗<我知道RxJava可以很好地进行改装。我应该放弃Parse SDK,转而使用他们的REST API吗? 帮帮我,

  • 问题内容: 我的Select2 3.4.5无法使用JSON数据。 这是我在HTML上的输入框: …还有我的JavaScript 我使用Laravel 4制作了一个API,每当在文本框中键入任何内容时,该API都会返回一个值。 如果在文本框中键入“ test”,则结果如下: 我无法将结果添加到我的Select2下拉列表中。我认为并且正在引起问题,因为我不知道应该在上面放置什么参数。我不知道从何处获取

  • 我的Select2 3.4.5不适用于JSON数据。 这是我在超文本标记语言上的输入框: ...和我的JavaScript 我用Laravel 4制作了一个API,每当我在文本框中键入任何内容时,它都会返回一个值。 如果我在输入框上键入“test”,结果如下: 我无法将结果添加到Select2下拉列表中。我认为是formatSelection和formatResult导致了这个问题,因为我不知道应

  • 我有个大麻烦。我必须将应用程序中的一些参数发布到URL。但是,当我试图在WebView对象中执行此操作时,它会向我报告一个异常,该异常表示“不受信任的证书”(该异常由WebViewClient onReceivedSslError()的重写中的方法报告)。如何才能与服务器正确握手?你能给我一些提示吗?我快疯了... 真的,真的谢谢... 编辑:这是我定义webview的方式 这就是我在WebVie