使用Ksoap2调用Web Service加入SoapHeader

关于这个问题,如果使用百度都是前篇一律的代码,好不容易上了google才找到完整的方法,这里讲所有的代码都贴出来与大家分享。

首先是.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调用Web Service加入SoapHeader_第1张图片

随后就是使用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了,能够接收到服务端回发的用户数据

 

你可能感兴趣的:(Web,service)