一个 .net 2.0 的 Web Service 是否总是使用 Soap over http 协议?

3

我正在进行研究,下载了一个调用标准.asmx服务的测试应用程序。该服务使用标准的POST请求进行调用。我有些困惑,因为我认为.asmx服务总是使用SOAP协议?还是最近才引入了与HTTP(POST)通信的功能?

3个回答

1
不,ASMX Web服务并不限于SOAP。您可以使用ScriptMethodAttribute为web方法指定HTTP动词。这是在 .Net 3.5 中引入的。例如:
[ScriptMethod(UseHttpGet = true)]
public string MyMethod()
{
   return "Hello World";
}

1

.NET Web服务使用您选择的一种协议。默认情况下是SOAP,并允许POST请求。

.NET自动创建的标准帮助页面:

POST /demo/MSDN/PerfCounter.asmx HTTP/1.1
Connection: Keep-Alive
Content-Length: 150
Content-Type: text/xml
Host: localhost
User-Agent: MS Web Services Client Protocol 1.0.2204.19
SOAPAction: "http://tempuri.org/PerfCounters"

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
               xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/1999/XMLSchema">
  <soap:Body>
    <PerfCounters xmlns="http://tempuri.org/"/>
  </soap:Body>
</soap:Envelope>

你也可以启用GET方法:

<configuration>
    <system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    </system.web>
</configuration>

这适用于 .NET 1.1


0

SOAP是一种标准,您可以选择使用它。它基于XML。如果是一些简单的东西,那么我会使用JSON。Web服务不仅限于POST。当您运行创建/更新/删除例程时,应该使用POST,而在运行数据检索例程时应该使用GET。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接