一、.asmx是什麼?
.asmx是一種基於HTTP協議的Web服務文件的擴展名,可以運用於ASP.NET平台,通過使用該擴展名的文件,可以構建出一種具有可被其他系統或程序調用的Web服務,使得不同的系統、程序之間可以相互調用,實現數據的共享與交換。
ASMX提供了用於Web服務的訪問和調用的SOAP(簡單對象訪問協議)API,SOAP是一種基於XML的協議,主要用於Web服務之間的通信,同時也支持其他協議,例如HTTP和SMTP。ASMX不僅能夠構建出SOAP風格的Web服務,還能夠構建出REST風格的Web服務,用於處理非XML數據。
二、.asmx的優點
ASMX具有以下幾個優點:
1. 開發簡單
ASMX是基於.NET框架的,因此它集成在Visual Studio的開發環境中,具有良好的開發工具和強大的功能庫,因此開發人員可以快速地開發和部署Web服務。
2. 跨平台性好
ASMX是基於標準的XML和HTTP協議的Web服務,因此可以在任何支持XML和HTTP協議的系統中運行,例如.NET和Java等。
3. 安全性高
ASMX使用HTTPS和數字證書進行數據安全加密和身份驗證,可以有效保護Web服務中的數據和業務邏輯。
4. 全局可用性
ASMX可以讓不同語言和平台之間的應用程序相互通信和交互,使得數據共享和業務邏輯可以在不同的應用程序中得以復用。
三、.asmx使用的範圍和場景
1. 範圍
ASMX可以應用於Web上的任何地方,例如公司內部的Web應用程序、公共網站和公司之間的交互性應用程序等,也可以應用於移動應用程序和嵌入式系統中。
2. 場景
ASMX的應用場景包括但不限於以下幾個方面:
(1) 提供Web服務
在Web服務中,ASMX可以作為一種Web服務文件類型,用來提供服務端的數據查詢和處理功能,向客戶端提供標準的API接口。
<%@ WebService Language="C#" Class="WebService1" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService1 : System.Web.Services.WebService {
[WebMethod]
public int Add(int a, int b) {
return a + b;
}
}
(2) 實現跨域訪問
如果應用程序部署在不同的域名下,瀏覽器會限制跨域訪問,為了解決這個問題,可以通過Jquery對ASMX文件進行請求,實現跨域訪問。
$<script>
var data = {
name: "張三",
age: 18
};
$.ajax({
type: "post", // 數據提交方式
contentType: "application/json", // 接收返回數據的格式
url: "http://localhost:8088/HelloWebService.asmx/Hello", // 接口地址
data: JSON.stringify(data), // 請求數據
dataType: "json", // 返回數據的內容格式
success: function (res) { // 請求成功的回調函數
console.log(res);
},
error: function (err) { // 請求失敗的回調函數
console.log(err);
}
});
</script>
[WebMethod]
public String Hello(String name, int age)
{
// 根據接收到的參數返回不同的信息
return "Hello, " + name + ", your age is " + age;
}
(3) 調用其他Web服務
可以通過ASMX文件來調用其他的Web服務文件,實現對服務的調用和利用。
var soap = "<?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>
<Add xmlns='http://tempuri.org/'>
<a>3</a>
<b>5</b>
</Add>
</soap:Body>
</soap:Envelope>";
$.ajax({
url: "http://localhost:8088/HelloWebService.asmx?op=Add",
type: "POST",
dataType: "xml",
contentType: "text/xml; charset=utf-8",
data: soap,
success: function(xml) {
console.log(xml); // 獲得返回的xml數據
},
error: function(err) {
console.log(err);
}
});
四、.asmx的缺點
ASMX的缺點主要包括:
1. 受限於HTTP和SOAP協議的限制
ASMX是基於HTTP協議和SOAP協議的,因此性能受到HTTP協議和SOAP協議的限制,請求和響應需要進行反序列化和序列化。
2. 不支持XML序列化
ASMX不支持XML序列化,因此在使用ASMX的過程中,需要遵循一定的數據格式規範,不能隨意改變數據格式。在處理複雜數據類型時,會存在一定的問題。
3. 無法處理JSON和REST協議
ASMX主要支持SOAP協議,不支持其他協議,例如REST和JSON等,不能滿足一些特定需求。
五、.asmx的實例:明文加密和解密
以下是明文加密和解密的ASMX文件示例:
<%@ WebService Language="C#" Class="EncryptAndDecrypt" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class EncryptAndDecrypt : System.Web.Services.WebService {
[WebMethod]
public string Encrypt(string inputString)
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(inputString);
data = new System.Security.Cryptography.SHA256Managed().ComputeHash(data);
return System.Text.Encoding.UTF8.GetString(data);
}
[WebMethod]
public string Decrypt(string inputString)
{
byte[] data = System.Convert.FromBase64String(inputString);
return System.Text.Encoding.UTF8.GetString(data);
}
}
原創文章,作者:JAHUQ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/351745.html