Web Services Android作为Web服务客户端站点,用于通过JAX-WS进行应用程序身份验证

pxq42qpu  于 2022-11-15  发布在  Android
关注(0)|答案(1)|浏览(135)

因此,我偶然发现了这个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进行应用程序身份验证呢?感谢您的建议。提前感谢

pbwdgjma

pbwdgjma1#

是的,终于做到了!我要把我的代码贴在这里,帮助像我这样的迷途羔羊。

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!

我是从https://stackoverflow.com/a/7576428/6318215得到这个想法的
P/S:我使用envelope.dotNet = false,因为我的webService不能在Microsoft的.NET框架上运行

相关问题