簡體   English   中英

如何將數據從網頁發送到XML,然后再發送到SOAP Web服務

[英]How to send data from a web page to XML and then to a SOAP web service

到目前為止,我們為應用程序提供了一個后端SOAP Web服務,該服務使用xml輸入來實現該服務。 但是現在,我們正在嘗試構建前端,該前端從用戶那里獲取一些輸入,並僅相應地更新xml原子值,因為我們已經具有xml結構。 從用戶值更新xml后,服務應以該xml作為輸入命中。

因此流程如下所示:用戶輸入(html)->更新xml->將此xml作為輸入傳遞給SOAP Web服務---服務響應。

我不知道可以使用什么技術來完成這項工作,如何更新以php和xpath開頭的xml,不知道我是否正確。 誰能幫我這個忙,並提出解決方案?

另外,我的xml具有soap標頭,因此我在將此XML與xpath結合使用時遇到麻煩,因為xpath使用純xml。 有什么解決辦法嗎?

java,springs用於后端開發。

如果您具有Java平台,那么此解決方案將對您有所幫助。 為此,我創建了一個新的Web應用程序項目,並在Web服務類別中使用了Web Service Wrom WSDL向導。 實現很容易:

@WebService(serviceName = "AddNumbersService", portName = "AddNumbersPort", endpointInterface = "org.example.duke.AddNumbersPortType",
            targetNamespace = "http://duke.example.org", wsdlLocation = "WEB-INF/wsdl/AddNumbersImpl/AddNumbers.wsdl")
public class AddNumbersImpl implements AddNumbersPortType {

    public int addNumbers(int arg0, int arg1) throws AddNumbersFault {
        int result = arg0+arg1;
        if (result < 0) {
            org.example.duke.xsd.AddNumbersFault fault = new org.example.duke.xsd.AddNumbersFault();
            fault.setMessage("the result is negative");
            fault.setFaultInfo("negative result: "+result);
            throw new AddNumbersFault("error", fault);
        } else {
            return result;
        }
    }

    public void oneWayInt(int arg0) {
        System.out.println("JAX-WS: oneWayInt request "+arg0);
    }

}

在不知道您正在使用的確切環境(目標Web服務,首選編程語言等)的情況下,很難提供准確的答案。 但我會嘗試一個一般性的答案。

消耗Web服務

SOAP Web服務經常(總是?)提供機器可讀的描述,該描述可以被某些工具“消耗”,以您喜歡的語言生成代碼以與API交互。

例如,Salesforce以WSDL(Web服務描述語言)文件的形式提供針對給定帳戶定制的SOAP API的描述。 它是XML,就像SOAP請求/響應一樣。 對於我的應用程序,我已經使用Visual Studio在項目中使用WSDL文件創建對Salesforce API的引用。 其余的操作通過簡單的對象初始化和方法完成。 例如:

using ServiceName;  // Namespace generated by Visual Studio using the WSDL.

var client = new ServiceClient("username", "password");

client.Create(new ServiceThing("information", "about the new thing"));

此示例使用類似於C#的語法,將調用SOAP API來創建具有給定信息的對象。 網絡請求的詳細信息被完全隱藏。

要了解如何使用目標API的服務,請搜索諸如“在[您選擇的語言]中使用WSDL”之類的內容。 更好的是,查看目標Web服務是否具有描述如何使用它的文檔。

從網頁發送數據

有許多工具可以從HTTP POST請求中檢索信息並對其執行操作。 我對ASP.NET最熟悉。 您似乎對PHP很熟悉。

為簡單起見,您將嘗試使用與接收來自用戶輸入的Web服務相同的語言來使用目標Web服務。 我將提供一個類似ASP.NET的示例,希望您可以將其轉換為首選工具:

protected void Page_Load(object sender, EventArgs e) 
{
    if (Page.IsPostback)
    {
        // a and b are names of input fields in our page.
        string a = Request["a"];
        string b = Request["b"];

       // Let's assume we've initialized the web service client as a property.
       Client.Create(new ServiceThing(a, b));
    }
}

妳去 無需手動進行XML編輯。 SOAP XML是根據ServiceThing對象自動生成的,並發送到目標Web服務。

同樣,這是對一般問題的一般回答。 如果您使用更具體的內容更新問題,我將嘗試使用更具針對性的詳細信息來編輯此答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM