android调用webservice带soapheader验证

本文介绍了一个使用SOAP协议进行认证并从服务器获取数据的Android应用实例。客户端通过自定义SOAP头传递认证信息,并在后台线程中异步发起请求。服务器端通过.NET WebService实现,对SOAP头部信息进行验证。
摘要由CSDN通过智能技术生成

android端代码//需要ksoap2jar包

public class myActivity extends Activity
{

   private TextView textview1;

   private Handler mHandler=new Handler(){
       public void handleMessage(Message msg){
            switch(msg.what){
                 case 0X100:
                     String result=(String)msg.obj;
                      textview1.setText(result);
                      break;
                default:
                     super.handleMessage(msg);
           }
        }
    };

    protected void onCreate(Bundle savedInstanceState){ 

          super.onCreate(savedInstanceState);
          setContentView(R.layout.mydata);

          //在新线程中获取远程数据

          new Thread()
          { 
            public void run() {  
                Message msg = Message.obtain(mHandler); 
                msg.obj = getInfo("x","y");
                msg.what=0x100;
                mHandler.sendMessage(msg); 
               
            }; 
          }.start();

    }

    public String getInfo(String para1,String para2){

          String nameSpace = "http://tempuri.org/";
          String methodName = "getMyData";
          String endPoint = "http://abc.edu.cn/webservice1.asmx";
          String soapAction = "http://tempuri.org/getMyData";
         //建立soapheader,用header传UserName和PassWord
          Element[] header = new Element[1];
          header[0] = new Element().createElement(nameSpace, "MySoapHeader");
          Element uname = new Element().createElement(nameSpace, "UserName");
          uname.addChild(Node.TEXT, "xxx");//<UserName>xxx</UserName>
          header[0].addChild(Node.ELEMENT, uname);
    
          Element pass = new Element().createElement(nameSpace, "PassWord"); 
          pass.addChild(Node.TEXT, "xxx");;//<PassWord>xxx</PassWord>
          header[0].addChild(Node.ELEMENT, pass); 
 
           SoapObject so = new SoapObject(nameSpace, methodName);
           so.addProperty("p1", para1);
           so.addProperty("p2", para2);

          SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER12);
          envelope.headerOut=header;
          envelope.bodyOut=so;
          envelope.dotNet=true;
          envelope.setOutputSoapObject(so);
          HttpTransportSE transport=new HttpTransportSE(endPoint);
          try {
    
               transport.call(soapAction, envelope);// 调用WebService
       
          } catch (Exception e) {
                e.printStackTrace();
          }

         SoapObject object=(SoapObject)envelope.bodyIn;// 获取返回的数据

                SoapObject anytype=(SoapObject)object.getProperty(0); //anytype={Result=xxxxxx,......}
                String rs=anytype.getProperty("Result").toString();

         return rs;

    }

}

服务端

using ......

[WebService(Namespace = http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

public class WebService1 : System.Web.Services.WebService
{
    public MySoapHeader myHeader;

    public WebService1()
    {
    }


    [WebMethod]
    [SoapHeader("myHeader",Direction=SoapHeaderDirection.In)]//android端传给服务端验证

    //[SoapHeader("myHeader",Direction=SoapHeaderDirection.Out)]//服务端给android端验证,需要创建header

    public myClass getMyData(string para1,string para2)
    {

        myClass obj = new myClass();
        if (myHeader == null)
        {
            obj.Result = "无法调用此服务!";

            return obj;
        }
        if (myHeader.UserName.Equals("xxx") & myHeader.PassWord.Equals("xxx"))
        {

           //你的代码

            return obj;
        }
        else
        {
           obj.Result = "无权调用此服务!";
           
            return obj;
        }
       
    }
  

    public class myClass
    {

        public string Result{get;set;}
        public string Other{ get; set; }
        public myClass()
        {
           
            Result = "null";
            Other = "null";
        }
    }


    public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
    {

       //名称必须与android端一样

       public string UserName;
       public string PassWord;

    }

}


请求

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns=“http://tempuri.org/”>
      <UserName>string</UserName>
      <PassWord>string</PassWord>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <getMyData xmlns=“http://tempuri.org/”>
      <p1>string</p1>
      <p2>string</p2>
    </getMyData>
  </soap:Body>
</soap:Envelope>

响应

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMyDataResponse xmlns="http://tempuri.org/">
      <getMyDataResult>
        <Result>string</Result>
        <Other>string</Other>
      </getMyDataResult>
    </getMyDataResponse>
  </soap:Body>
</soap:Envelope>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值