🎬 HoRain 云小助手个人主页

⛺️生活的理想,就是为了理想的生活!


⛳️ 推荐

前些天发现了一个超棒的服务器购买网站,性价比超高,大内存超划算!忍不住分享一下给大家。点击跳转到网站。

目录

⛳️ 推荐

WebService SOAP1.1与SOAP1.2的HTTP POST方式调用

基本概念

主要区别

1. Content-Type 设置

2. SOAPAction 头部字段

3. 错误处理机制

4. 数据类型和编码支持

HTTP POST调用示例

SOAP1.1调用示例

SOAP1.2调用示例

实际调用方法

1. 获取SOAPAction

2. C# HTTP POST调用示例

3. Java调用示例

选择建议

总结


WebService SOAP1.1与SOAP1.2的HTTP POST方式调用

基本概念

SOAP(Simple Object Access Protocol,简单对象访问协议)是一种用于交换结构化信息的协议,主要用于实现网络服务。SOAP可以与各种通信协议一起使用,包括HTTP、SMTP、TCP、UDP等。SOAP1.1和SOAP1.2是该协议的两个主要版本,它们在HTTP POST方式调用上存在一些关键区别。

主要区别

1. Content-Type 设置

特性 SOAP1.1 SOAP1.2
Content-Type text/xml application/soap+xml
说明 传统媒体类型,用于标识XML消息 新的媒体类型,专门用于标识SOAP消息

2. SOAPAction 头部字段

特性 SOAP1.1 SOAP1.2
SOAPAction 头部 必需 不再需要
替代方案 引入了"action"参数,作为Content-Type头部的一部分
示例 SOAPAction: "http://tempuri.org/HelloWorld3" Content-Type: application/soap+xml; action="http://tempuri.org/HelloWorld3"

3. 错误处理机制

特性 SOAP1.1 SOAP1.2
错误处理 所有错误都返回HTTP 500状态码 只有当真正的网络问题发生时,才返回HTTP 500状态码
优势 无法区分网络问题和SOAP问题 能够区分网络问题和SOAP问题,提供更好的错误诊断

4. 数据类型和编码支持

特性 SOAP1.1 SOAP1.2
数据类型 有限支持 支持更多数据类型
编码风格 有限支持 支持更多编码风格,提供更大的灵活性
规范 较为宽松 规范更加严格和详细,提高互操作性

HTTP POST调用示例

SOAP1.1调用示例

POST /WebService1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld3"

<?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>
    <HelloWorld3 xmlns="http://tempuri.org/">
      <a>10</a>
      <b>World</b>
    </HelloWorld3>
  </soap:Body>
</soap:Envelope>

SOAP1.2调用示例

POST /WebService1.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; action="http://tempuri.org/HelloWorld3"
Content-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://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
    <HelloWorld3 xmlns="http://tempuri.org/">
      <a>10</a>
      <b>World</b>
    </HelloWorld3>
  </soap:Body>
</soap:Envelope>

实际调用方法

1. 获取SOAPAction

SOAPAction值可以从WSDL文件中获取:

  • 浏览器访问接口地址后加?wsdl,获取WSDL文件
  • 在WSDL文件中查找<soap:address location="..."><soap:operation soapAction="...">节点

2. C# HTTP POST调用示例

// SOAP1.1调用
string str = WebHelper.LoadXmls("<request><webSl>123456</webSl></request>");
Hashtable header = new Hashtable();
header.Add("SOAPAction", "http://tempuri.org/***");
var res = WebHelper.Http("http://yourwebserviceurl", str, header);

// WebHelper.Http方法实现
public static string Http(string url, string data = null, Hashtable header = null)
{
    string method = "post";
    string contenttype = "text/xml;charset=utf-8";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = string.IsNullOrEmpty(method) ? "post" : method;
    request.ContentType = string.IsNullOrEmpty(contenttype) ? "text/xml;charset=utf-8" : contenttype;
    
    if (header != null)
    {
        foreach (var i in header.Keys)
        {
            request.Headers.Add(i.ToString(), header[i].ToString());
        }
    }
    
    if (!string.IsNullOrEmpty(data))
    {
        Stream RequestStream = request.GetRequestStream();
        byte[] bytes = Encoding.UTF8.GetBytes(data);
        RequestStream.Write(bytes, 0, bytes.Length);
        RequestStream.Close();
    }
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream ResponseStream = response.GetResponseStream();
    // 处理响应...
}

3. Java调用示例

// SOAP1.1调用
URL url = new URL("http://yourwebserviceurl");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("SOAPAction", "http://tempuri.org/HelloWorld3");
connection.setDoOutput(true);

String soapRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<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/\">\n" +
                    "  <soap:Body>\n" +
                    "    <HelloWorld3 xmlns=\"http://tempuri.org/\">\n" +
                    "      <a>10</a>\n" +
                    "      <b>World</b>\n" +
                    "    </HelloWorld3>\n" +
                    "  </soap:Body>\n" +
                    "</soap:Envelope>";

try (OutputStream os = connection.getOutputStream()) {
    os.write(soapRequest.getBytes());
}

// 处理响应...

选择建议

  1. 当使用SOAP1.2时

    • 选择标准:如果您的Web服务支持SOAP1.2,建议使用SOAP1.2
    • 优势:更好的错误处理、更丰富的数据类型支持、更严格的规范
    • 适用场景:新开发的Web服务,需要更高安全性和互操作性的环境
  2. 当使用SOAP1.1时

    • 选择标准:如果您的Web服务只支持SOAP1.1,或者需要与旧系统兼容
    • 优势:广泛支持,特别是与旧版.NET和Java Web服务兼容
    • 适用场景:需要与遗留系统集成,或Web服务提供商仅支持SOAP1.1

总结

SOAP1.1与SOAP1.2在HTTP POST方式调用上的主要区别在于:

  • Content-Type:SOAP1.1使用text/xml,SOAP1.2使用application/soap+xml
  • SOAPAction:SOAP1.1需要必需的SOAPAction头部,SOAP1.2不再需要,而是将action作为Content-Type的一部分
  • 错误处理:SOAP1.2提供更好的错误处理机制
  • 数据类型支持:SOAP1.2支持更多数据类型和编码风格

在实际应用中,应根据Web服务的实际支持情况来选择使用哪个版本。如果Web服务支持SOAP1.2,建议优先使用SOAP1.2以获得更好的功能和安全性。

❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄

💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍

🔥🔥🔥Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!🌙🌙🌙

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐