gsoap工具生成wsdl接口 注意事項

wsdl是經過wsdl文件做爲不一樣應用的通訊接口,因此如何生成wsdl語言很重要,可是不少時候咱們發現本身編寫的頭文件經過gsoap工具soapcpp2.exe從頭文件中沒法正常生成對應的wsdl語言,幾經尋找,終於發現:javascript

 經過對應頭文件.h生成客戶端或服務器代理代碼的時候,對應的頭文件中的註釋描述信息和對外接口都須要有規定的格式才能正確生成wsdl語言文件。html

一、如過頭文件中導出函數命名沒有前綴,那麼能生成對應的客戶端或服務器代理相關代碼,可是沒法爲每個接口生成對應的請求和響應wsdl文件,不然,必須添加ns__前綴,注意是兩條下劃線才能夠生成,且生成的schema對應的域名爲默認ns.xsdjava

頭文件:jquery

#ifndef _C_WEB_CLIENT_INTERFACE_web

#define _C_WEB_CLIENT_INTERFACE_瀏覽器

int ns__Add( int num1, int num2, int* sum );服務器

int ns__Sub(int num1, int num2, int* sum);app

int ns__Mul(char *namelist, char **arealist);ssh

#endif異步

 

生成命令:

soapcpp2 -c test.h -1

 

生成結果包含各個接口的請求和響應wsdl文件,例如Add方法的請求wsdl: ns.Add.req.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope

 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 xmlns:ns="http://tempuri.org/ns.xsd">

 <SOAP-ENV:Body>

  <ns:Add>

   <num1>0</num1>

   <num2>0</num2>

  </ns:Add>

 </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

 

Add方法的響應wsdl文件:ns.Add.res.xml

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope

 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 xmlns:ns="http://tempuri.org/ns.xsd">

 <SOAP-ENV:Body>

  <ns:AddResponse>

   <sum>0</sum>

  </ns:AddResponse>

 </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

 

二、加入帶註釋的wsdl配置項目

 

#ifndef _C_WEB_CLIENT_INTERFACE_

#define _C_WEB_CLIENT_INTERFACE_

 

// wsdl配置項

//gsoap ns service name: name_area

//gsoap ns service style: rpc

//gsoap ns service encoding: encoded

//gsoap ns service namespace: http://127.0.0.1:9000/name_area.wsdl

//gsoap ns service location: http://127.0.0.1:9000/name_area

//gsoap ns schema  namespace:    urn:name_area

 

int ns__Add( int num1, int num2, int* sum );

int ns__Sub(int num1, int num2, int* sum);

int ns__Mul(char *namelist, char **arealist);

 

#endif

 

生成add方法的請求wsdl文件名:name_area.Add.req.xml

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope

 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 xmlns:ns="urn:name_area">

 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

  <ns:Add>

   <num1>0</num1>

   <num2>0</num2>

  </ns:Add>

 </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

 

生成add方法的響應wsdl文件名:name_area.Add.res.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope

 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 xmlns:ns="urn:name_area">

 <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

  <ns:AddResponse>

   <sum>0</sum>

  </ns:AddResponse>

 </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

 

總結: 要想生成對應的wsdl文件,接口命名必須以ns__前綴打頭,其次要控制對應wsdl文件文件名和編碼等配置,必需要有對應的註釋配置項目,soapcpp2工具會自動檢查對應配置生成對應的文件;

--------------------- 

轉自:https://blog.csdn.net/sunxiaopengsun/article/details/77069126 

 

 

做爲一個java開發人員,當咱們獲取到了對方提供的wsdl地址,而後在網頁上可以訪問wsdl文檔之後,如何調用對方的webservic藉口呢?一下有幾種方法,能夠參考一下。第一種是      HttpURLConnection方式第二種是      JSP頁面上純JS調用內容說明一、WSDL文檔基本說明二、httpURLConnection方式調用三、JS方式調用四、獲取的數據如何解析?一、WSDL文檔基本說明接口文檔以下:如下是公共對外開放的查詢手機號歸屬地的一個藉口文檔:WSDL:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl能夠本身用瀏覽器打開訪問一下;一眼望去,可能接口看起來很複雜,較爲繁瑣。  可是做爲開發人員,使用的時候,  咱們能夠查看關注點信息,就能實現接口的調用了!!那麼咱們須要瞭解嗎哪些信息呢?從上面的文檔裏面,咱們須要從頭部(最上面一行)瞭解到:系統的targetNamespace="http://WebXml.com.cn/"從<wsdl:types>標籤裏的<s:element ></s:element>的name=「xxxxxxx」,獲知該文檔提供了兩個方法:getMobileCodeInfo、getDatabaseInfo(後面帶Response的是對應方法的回覆)從底部的<wsdl:service name="MobileCodeWS"></wsdl:service>標籤裏面,能獲知<http:address location="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"/>請求地址。http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx通常而言,知悉以上信息,你就能調用藉口了。須要傳參數調用的,可能在業務上須要與對方瞭解一下,參數的格式(數據類型,文檔裏是能夠知曉的)二、httpURLConnection方式調用代碼以下:package com.ssh.webserviceTSY; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL; public class Test {    public static void main(String[] args) throws IOException {          //第一步:建立服務地址          URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");          //第二步:打開一個通向服務地址的鏈接          HttpURLConnection connection = (HttpURLConnection) url.openConnection();          //第三步:設置參數          //3.1發送方式設置:POST必須大寫          connection.setRequestMethod("POST");          //3.2設置數據格式:content-type          connection.setRequestProperty("content-type", "text/xml;charset=utf-8");          //3.3設置輸入輸出,由於默認新建立的connection沒有讀寫權限,          connection.setDoInput(true);          connection.setDoOutput(true);            //第四步:組織SOAP數據,發送請求          String soapXML = getXML("17321242779");          //將信息以流的方式發送出去        OutputStream os = connection.getOutputStream();          os.write(soapXML.getBytes());          //第五步:接收服務端響應,打印          int responseCode = connection.getResponseCode();          if(200 == responseCode){//表示服務端響應成功              //獲取當前鏈接請求返回的數據流            InputStream is = connection.getInputStream();              InputStreamReader isr = new InputStreamReader(is);              BufferedReader br = new BufferedReader(isr);                            StringBuilder sb = new StringBuilder();              String temp = null;              while(null != (temp = br.readLine())){                  sb.append(temp);              }                          /**             * 打印結果             */            System.out.println(sb.toString());                          is.close();              isr.close();              br.close();          }          os.close();      }        public static String getXML(String phone){          String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"              +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2003/XMLSchema-instance\" "     +"xmlns:web=\"http://WebXml.com.cn/\"  "             +"xmlns:xsd=\"http://www.w3.org/2003/XMLSchema\" "     +"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"                  +"<soap:Body>"                    +"<web:getMobileCodeInfo>"                      +phone                  +"</web:getMobileCodeInfo>"                  +"</soap:Body>"              +"</soap:Envelope>";          return soapXML;      }  }說明 : 通常的HTTP請求大部分都是相同的,可是因爲webservice是基於soap協議的,因此,請求時的文本格式須要設置好。文中的soapXML信息,須要注意xmlns:web=「tns」 這個xml命名空間, 這個空間是依據wsdl文檔裏的頭部NameSpace信息,肯定的程序會依據這個名稱,檢索到該文檔裏面的方法,譬如:<web: getDatabaseInfo> 獲取getDatabaseInfo方法 (web是xmlns的名字,能夠自定義,至關於「id」)三、JS方式調用<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Test WebService</title><script type="text/javascript" src="js/jquery-2.1.1.min.js"></script><script>$(function(){invoke();back();});var xhr;function invoke(){    if(window.ActiveXObject){        xhr = new ActiveXObject("Microsoft.XMLHTTP");    }else{        xhr = new XMLHttpRequest();    }    //指定請求地址    var url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";        //定義請求類型和地址和異步,通常都是post方式。    xhr.open("POST", url, true);    //設置Content-Type    xhr.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");    //指定回調方法    xhr.onreadystatechange = back;    //組裝消息體的數據    var data =     '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '    ' xmlns:web="http://WebXml.com.cn/" '    ' xmlns:xsd="http://www.w3.org/2003/XMLSchema" '    ' xmlns:xsi="http://www.w3.org/2003/XMLSchema-instance">'    +'<soapenv:Body>'    +'<web:getDatabaseInfo>'    +'<data>'    +'17321242719'    +'</data>'    +'</web:getDatabaseInfo>'    +'</soapenv:Body>'    +'</soapenv:Envelope>';    xhr.send(data); }function back(){    if(xhr.readyState == 4){        if(xhr.status == 200){            var doc = xhr.responseXML;            alert(xhr.getAllResponseHeaders());          //  xhr.getResponseBodyAsStream            alert(xhr.responseText);                    }    }/* else{    alert("請求失敗")    } */}</script></head><body></body></html>--------------------- 做者:森雅香 來源:CSDN 原文:https://blog.csdn.net/myblogzz/article/details/80311038 版權聲明:本文爲博主原創文章,轉載請附上博文連接!

相關文章
相關標籤/搜索