Android 作为使用 JAX-WS 进行应用程序身份验证的 Web 服务客户端站点

Android as web service client site for Application Authentication with JAX-WS

因此,我遇到了这个 website 以了解使用 JAX-WS 进行应用程序身份验证。我试图在 android 中应用它,其中 android 充当 Web 服务客户端。

在提供的示例中,服务函数用于访问 url。

Service service = Service.create(url, qname);

虽然下面的代码用于传递用户名和密码:

Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);

Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("mkyong"));
headers.put("Password", Collections.singletonList("password"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

我的问题是,由于 android 不支持 javax.xml.ws.Service,那么我应该如何使用 android 通过 JAX-WS 进行应用程序身份验证? 感谢您的建议。提前致谢

是的,终于搞定了! 我要 post 我的代码在这里帮助像我一样迷路的羊。

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;



public class SampleCode extends AppCompatActivity {

    private final String NAMESPACE = "http://ws.mkyong.com/";
    private final String SOAP_ACTION = "\"http://ws.mkyong.com/getHelloWorldAsString\"";
    private final String METHOD_NAME = "getHelloWorldAsString";
    private final String URL_App = "http://localhost:8080/JAX-WS-Application-Authentication-Example/HelloWorldImplService?WSDL";

    String TAG = "Repsonse";
    String response = "";

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

        Button btnClick = (Button) findViewById(R.id.btnClick2);
        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncCallWS().execute();
            }
        });
    }

    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            sendHeader();
            return null;
        }
    }


    private void sendHeader(){

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_App);
        try{
            envelope.setOutputSoapObject(request);

            List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
            headerList.add(new HeaderProperty("Username", "mkyong"));
            headerList.add(new HeaderProperty("Password", "password"));

            androidHttpTransport.call(SOAP_ACTION, envelope, headerList);

            SoapPrimitive resultString = (SoapPrimitive) envelope.getResponse();


            Boolean status = Boolean.valueOf(resultString.toString());

            Log.i(TAG, "Result : " + resultString );

        }catch (Exception ex) {
            Log.e(TAG, "Exception: " + ex);
            response = "Exception";
        }
    }
}

结果如下图:

I/Repsonse: Result : Hello World JAX-WS - Valid User! 

我的想法来自

P/S:我正在使用 envelope.dotNet = false 因为我的 webService 在 Microsoft 的 .NET 框架

上没有 运行