简体   繁体   English

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

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

So, I came across this website to learn about Application Authentication with JAX-WS. 因此,我浏览了该网站,以了解有关JAX-WS的应用程序身份验证的信息。 I am trying to apply it in android where android acts as the web service client. 我正在尝试将其应用在android充当网络服务客户端的android中。

In the example provided, service function is used to access the url. 在提供的示例中,服务功能用于访问URL。

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

While the below code is used to pass username and password: 虽然以下代码用于传递用户名和密码:

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

My question is, since android does not support javax.xml.ws.Service, then how should I approach Application Authentication with JAX-WS using android? 我的问题是,由于android不支持javax.xml.ws.Service,那么我应该如何使用android使用JAX-WS进行应用程序身份验证? Appreciate the advice. 欣赏建议。 Thanks in advance 提前致谢

Yes, finally done it! 是的,终于做到了! I am gonna post my code here to help lost sheep like me. 我要在这里发布我的代码,以帮助像我这样的失羊。

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";
        }
    }
}

Result is as shown below: 结果如下图:

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

I get the idea from https://stackoverflow.com/a/7576428/6318215 我从https://stackoverflow.com/a/7576428/6318215获得了这个想法

P/S: I am using envelope.dotNet = false because my webService does not run on Microsoft's .NET framework P / S:我正在使用envelope.dotNet = false因为我的webService无法在Microsoft的.NET框架上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM