所以,我遇到了这个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进行应用程序身份验证? 欣赏建议。提前致谢
答案 0 :(得分:0)
是的,终于完成了! 我将在这里发布我的代码,以帮助像我一样迷失的羊。
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框架上运行