C# 肥皂

WBOY
發布: 2024-09-03 15:18:26
原創
1017 人瀏覽過

SOAP(簡單存取物件協議)是一種基於 XML 的協議,為以不同語言編寫並在不同平台上運行的應用程式提供相互互動的工具。它透過 HTTP 運行。 SOAP 是一種輕量級協議,因為它是基於 XML,而 XML 是一種輕量級語言。 C# SOAP 獨立於它所工作的平台和作業系統,這使得它更容易在不同平台上工作的不同應用程式之間交換資料。它是一種鬆散耦合的協議,因為它不需要通訊應用程式使用相同的語言。

文法

定義 SOAP 訊息的語法如下:

<SOAP : Envelope xmlns : SOAP = "https://www.educba.com/">
<SOAP : Header>
</SOAP : Header>
<SOAP : Body>
<SOAP : Fault>
</SOAP : Fault>
</SOAP : Body>
</SOAP : Envelope>
登入後複製

定義 SOAP 訊息的語法規則如下:

SOAP 訊息的編碼應使用 XML 語言完成。它應該使用 SOAP Envelope 命名空間。它不應包含 DTD 參考和 XML 處理指令。

SOAP 在 C# 中如何運作?

SOAP 適用於編組和解組機制。它使用 HTTP 協定將基於 XML 的訊息(稱為 SOAP 訊息)傳送到伺服器進行處理。這些 SOAP 訊息包含要處理的資訊。我們可以稱之為 HTTP 請求,這種將資訊包裝到 SOAP 訊息中的方法稱為編組。

現在,伺服器接收來自客戶端的請求並解開客戶端發送的 SOAP 訊息。然後,伺服器處理該請求並以 SOAP 訊息的形式向客戶端發送適當的回應。這種解包資訊的方法稱為解組。

C# 肥皂

SOAP 訊息的元素

Soap 訊息由以下元素組成:

1。 SOAP Envelope 元素:此元素是 SOAP 訊息的根元素。它告訴我們特定的 XML 文件是一個 SOAP 訊息。它包含 SOAP 訊息的詳細資訊。標頭元素:SOAP 標頭元素是 SOAP 訊息的可選元素。但如果 SOAP 訊息包含此元素,那麼它應該是根 Envelope 元素的第一個子元素,並且 Header 的子元素應該被限定為命名空間。此元素包含付款資訊、身份驗證憑證等資訊。 SOAP Body 元素:此元素包含要在兩個端點之間交換的實際資訊。它包含請求和回應訊息。

請在下方找到包含員工詳細資訊的 SOAP 回應訊息中的 SOAP Body 元素範例:

代碼:

<soap : Body>
<GetEmployeeDetails>
<EmployeeName>John Duffel</EmployeeName>
<EmployeeCode>EI66</EmployeeCode>
</GetEmployeeDetails>
</soap: Body>
登入後複製

2。 SOAP 錯誤元素: 當 SOAP 訊息傳送到伺服器時,伺服器傳回的回應可以包含在請求中成功處理要求所需的訊息,也可以包含錯誤訊息。因此,該元素包含與錯誤相關的資訊。如果 SOAP 訊息包含此元素,那麼它應該是 Body 元素的子元素。

Fault元素的子元素如下:

  • :此元素包含底層故障的程式碼,有助於我們識別故障。
  • :此元素包含一則文字訊息,提供故障的詳細說明。
  • :這是一個可選元素,告訴我們誰要為錯誤負責。
  • :這也是一個可選元素,包含特定於應用程式的錯誤訊息。

請在下方找到顯示 SOAP 訊息結構的圖表:

C# 肥皂

帶有彩色背景的元素是 SOAP 訊息的可選元素。

讓我們來看看用 C# 建立 SOAP Web 服務所需的步驟。步驟如下:

  • 在 Visual Studio 中,前往檔案 ->新->專案為 Web 服務建立新專案。
  • 選擇C#和Web模板;在其下,選擇 ASP.NET Web 應用程式。
  • 給出解決方案的名稱和位置。
  • 現在,該專案將出現在解決方案資源管理器中。
  • 在解決方案資源管理器中以滑鼠右鍵按一下該項目,然後選擇新增 ->網路服務 (ASMX)

在此服務文件中,您可以新增服務程式碼並執行它,如範例部分下的範例所示。

實作 C# SOAP 的範例

以下是提到的範例:

範例#1

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication4
{
[WebService(Name ="Sample Web Service")]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string Message()
{
return "Learning SOAP web service";
}
}
}
登入後複製

輸出:

C# 肥皂

點擊「訊息」(Web 方法)後,我們將得到以下輸出:

C# 肥皂

Example #2

The SOAP request and response in the above snapshot are as follows:

Code:

POST /WebService1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Message"
<?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>
<Message xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
登入後複製

In the above message, the first element is the Envelope element. Then this message contains the Body element, which provides details of the SOAP message.

Code:

HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8Content-Length: length 
<?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>    
<MessageResponse xmlns="http://tempuri.org/">      
<MessageResult>string</MessageResult>    
</MessageResponse>  
</soap:Body>
</soap:Envelope>
登入後複製

The first line of this message contains code ‘200’, which indicates a successful response from the server. This message contains an Envelope element and then a Body element containing details of the response from the server. We can see a tag ‘MessageResult’ with a value string, indicating that our Web Method (Message) result will be of a type string.

After clicking on the ‘Invoke’ button in the second snapshot, we will get the final result as shown below:

Output:

C# 肥皂

Conclusion

SOAP i.e. Simple Object Access Protocol, is a lightweight and loosely coupled protocol that can exchange data between applications written in different programming languages and working on different platforms. It exchanges data in the form of SOAP messages in XML language and works over HTTP protocol.

以上是C# 肥皂的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!