ANDROID調用webservice帶soapheader驗證

最近的一個項目中調用webservice接口,須要驗證soapheader,現將解決方法記錄以下:(網上資料出處太多,就不作引用,原做者如看到,若有必要添加請通知)android

一、先看接口web

POST /webserver/ValideWebService.asmx HTTP/1.1
Host: IP地址
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://命名空間/Login"

<?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://命名空間/">
      <ProjectID>string</ProjectID>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <Login xmlns="http://命名空間/">
      <loginName>string</loginName>
      <passowrd>string</passowrd>
    </Login>
  </soap:Body>
</soap:Envelope>

 

驗證時須要驗證header和body兩部分,須要引入第三方jar包,ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar。下面就是我驗證使用的方法,網上有許多,只不過沒法驗證,「拿來」修改一下,作個記錄,供之後查看,也方便你們參閱。網絡

先聲明如下;
//命名空間
    private static final String NAMESPACE = "http://命名空間/";
    //服務地址
    private static String URL = "http://IP地址或者域名/webserver/ValideWebService.asmx";
    //調用的方法名
    private static final String METHOD_NAME = "Login";
    //此處是命名空間+方法名
    private static String SOAP_ACTION = "http://命名空間/Login";
    private SoapObject  detail;

因爲2.3以上沒法在主線程中直接訪問網絡,因此在須要的地方開啓一個子線程,這裏我在點擊按鈕登陸的時候須要,所以寫在onclick()方法下:ide

new Thread() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub  
                    super.run();
                    try {
                        SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
                        //此處2個propertyinfo,是Login方法所需的參數,代碼下面貼出asmx代碼
                        PropertyInfo pi = new PropertyInfo();
                        pi.setName("loginName");
                        pi.setValue(cardNumStr);
                        rpc.addProperty(pi);
                        pi=new PropertyInfo();
                        pi.setName("passowrd");
                        pi.setValue(passwordStr);
                        rpc.addProperty(pi);
                        //soapheader在這裏
                        Element[] header = new Element[1];
                        header[0] = new Element().createElement(NAMESPACE, "MySoapHeader");
                        Element username = new Element().createElement(NAMESPACE, "ProjectID");
                        username.addChild(Node.TEXT, "這裏是值");
                        header[0].addChild(Node.ELEMENT, username);
                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                        envelope.headerOut = header;
                        envelope.bodyOut = rpc;
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(rpc);
                        HttpTransportSE ht = new HttpTransportSE(URL);
                        ht.call(SOAP_ACTION, envelope);
                        //SoapObject
                        detail =(SoapObject) envelope.getResponse();
                        System.out.println("返回的結果"+ detail.toString());
                    }catch (Exception e){
                        System.out.println("錯誤消息:"+ e.getMessage());
                    }
                    Message msg = handler.obtainMessage();
                    msg.obj=detail;
                    handler.sendMessage(msg);
                }
            }.start();

上面的cardNumStr和passwordStr是我從文本輸入框獲取的值。訪問網絡從接口經過驗證而後得到返回值,對返回的數據進行處理就能夠了。spa

用SoapObject,要不返回的detail爲null。線程

 

private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            //這裏作你的UI處理   
        };
    };
相關文章
相關標籤/搜索