使用Ksoap2调用Web Service加入SoapHeader
2014-09-19 11:01
351 查看
关于这个问题,如果使用百度都是前篇一律的代码,好不容易上了google才找到完整的方法,这里讲所有的代码都贴出来与大家分享。
首先是.NET写的后台代码
接下来是在浏览器中浏览查看我的接口,这里面有很重要的信息,包含了SoapHeader的名字以及参数
随后就是使用Ksoap2调用我的接口了
至此已经全部OK了,能够接收到服务端回发的用户数据
首先是.NET写的后台代码
/// <summary> /// SoapHeader /// </summary> public class OwnSoapHeader:SoapHeader { public string UserName { get; set; } public string Password { get; set; } } /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { public OwnSoapHeader OwnSoapHeader; [WebMethod] [SoapHeader("OwnSoapHeader")] public string Login() { if (OwnSoapHeader != null) { return OwnSoapHeader.UserName + "," + OwnSoapHeader.Password; } return "尚未登录"; } }
接下来是在浏览器中浏览查看我的接口,这里面有很重要的信息,包含了SoapHeader的名字以及参数
随后就是使用Ksoap2调用我的接口了
private void DoLogin() { new Thread() { @Override public void run() { Looper.prepare(); // TODO Auto-generated method stub String sNameSpace = "http://tempuri.org/"; String sMethodName = "Login"; String sActionString = "http://tempuri.org/Login"; String sURL = "http://192.168.1.101:8088/WebService1.asmx"; SoapObject rpc = new SoapObject(sNameSpace, sMethodName); //在这里加入了SoapHeader Element[] header = new Element[1]; //OwnSoapHeader与上图红色标记处名字一致 header[0] = new Element().createElement(sNameSpace, "OwnSoapHeader "); //UserName上图红色标记处名字一致 Element username = new Element().createElement(sNameSpace, "UserName"); username.addChild(Node.TEXT, "admin"); header[0].addChild(Node.ELEMENT, username); //Password上图红色标记处名字一致 Element pass = new Element().createElement(sNameSpace, "Password"); pass.addChild(Node.TEXT, "123"); header[0].addChild(Node.ELEMENT, pass); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER12); envelope.headerOut = header; envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc); HttpTransportSE ht = new HttpTransportSE(sURL); SoapObject soapObject = null; try { ht.call(sActionString, envelope); soapObject = (SoapObject) envelope.bodyIn; } catch (IOException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } catch (XmlPullParserException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } Bundle result = new Bundle(); if (soapObject != null) { result.putString("result", soapObject.toString()); } else { result.putString("result", sURL); } Message msg = new Message(); msg.setData(result); msg.what = 1; handler.handleMessage(msg); Looper.loop(); } }.start(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if (msg.what == 1) { Bundle result = msg.getData(); String text = result.getString("result"); Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG) .show(); } } };
至此已经全部OK了,能够接收到服务端回发的用户数据
相关文章推荐
- Android上使用ksoap2支持Web Service服务调用实例
- Android 使用 ksoap2-android调用Web Service学习
- Android使用Ksoap2调用Web service传递自定义对象,List,大文件
- 使用PB6.5调用Web Service的办法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(2):处理异步调用中的异常
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(3):保持用户上下文
- 4.3 使用AutoCompleteExtender异步调用Web service
- 不使用Orchestration实现Web Service调用
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 利用SoapHeader验证web service调用的合法性
- 在多线程环境下使用HttpWebRequest或者调用Web Service
- Orchestration调用带SOAPHeader的Web Service
- 使用Sharepoint的Lists Web Service调用UpdateListItems方法"增/删/改"List Items
- 利用SoapHeader验证web service调用的合法性(dwonmoon)
- 使用Axis开发Web Service应用及调用
- Flex 调用添加了SoapHeader的web service
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(5):服务器端和客户端数据类型的自动转换:基本类型和枚举类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet