android调用webservice带soapheader验证

来源:互联网 发布:闲鱼如何申请淘宝介入 编辑:程序博客网 时间:2024/05/16 11:47

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>


0 0
原创粉丝点击