SOAP with Attachments(简称SwA)是一种扩展SOAP协议的技术规范,主要用于在SOAP消息中附带非文本类型的附件数据,比如图片、文档、音频、视频等二进制内容,弥补了传统SOAP消息仅能传输结构化文本数据的不足。

SwA的核心概念
SwA的全称是SOAP with Attachments,由W3C组织提出,它的核心思路是将SOAP消息分为两部分:一部分是SOAP信封本身,包含XML格式的消息主体,用于描述业务逻辑相关的信息;另一部分是附件部分,存放需要传输的二进制或其他非XML格式的数据。两部分内容通过MIME多部分相关(multipart/related)的格式封装在同一个HTTP请求或响应中,实现消息和附件的同步传输。
和直接在SOAP消息体内用Base64编码嵌入二进制数据的方式相比,SwA不需要对附件进行编码转换,减少了数据体积和处理开销,更适合传输大尺寸附件。
SwA的工作流程
SwA的传输流程可以分为以下几个步骤:
- 客户端构造SOAP消息,将业务逻辑相关的参数放在SOAP信封的Body部分
- 将需要传输的附件文件准备好,记录附件的内容类型、标识符等信息
- 在SOAP消息中添加附件的引用信息,通常通过cid(Content-ID)来关联附件
- 按照multipart/related格式封装SOAP信封和所有附件,设置对应的MIME头信息
- 将封装后的整体内容通过HTTP协议发送给服务端
- 服务端接收到请求后,解析MIME多部分内容,分离出SOAP消息和附件,分别处理业务逻辑和附件数据
SwA的消息结构示例
以下是一个典型的SwA消息的HTTP请求结构示例,展示了SOAP消息和附件的封装方式:
POST /swa_endpoint HTTP/1.1
Host: ipipp.com
Content-Type: multipart/related; boundary=MIME_boundary; type="text/xml"; start="<soap-envelope@ippipp.com>"
Content-Length: 1024
--MIME_boundary
Content-Type: text/xml; charset=utf-8
Content-ID: <soap-envelope@ipipp.com>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<uploadFileRequest>
<fileName>test.jpg</fileName>
<fileRef>cid:attachment@ippipp.com</fileRef>
</uploadFileRequest>
</soap:Body>
</soap:Envelope>
--MIME_boundary
Content-Type: image/jpeg
Content-ID: <attachment@ipipp.com>
Content-Transfer-Encoding: binary
[这里是JPEG图片的二进制数据]
--MIME_boundary--
上面的示例中,boundary定义了多部分内容的分隔符,SOAP信封通过Content-ID标识,附件同样有独立的Content-ID,SOAP消息中通过cid:attachment@ippipp.com引用对应的附件。
SwA的实现示例
下面以Java语言为例,展示一个简单的SwA客户端发送带附件的SOAP请求的代码实现:
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
public class SwAClient {
public static void main(String[] args) throws Exception {
// 服务端地址
String endpoint = "http://ipipp.com/swa_endpoint";
// 附件文件路径
String attachmentPath = "D:/test.jpg";
// 生成唯一的分隔符和Content-ID
String boundary = "MIME_boundary_" + UUID.randomUUID().toString();
String soapCid = UUID.randomUUID().toString() + "@ipipp.com";
String attachCid = UUID.randomUUID().toString() + "@ipipp.com";
// 创建HTTP连接
URL url = new URL(endpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",
"multipart/related; boundary=" + boundary + "; type="text/xml"; start="<" + soapCid + ">"");
OutputStream out = conn.getOutputStream();
// 写入SOAP信封部分
String soapPart = "--" + boundary + "rn" +
"Content-Type: text/xml; charset=utf-8rn" +
"Content-ID: <" + soapCid + ">rnrn" +
"<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">rn" +
" <soap:Body>rn" +
" <uploadFileRequest>rn" +
" <fileName>test.jpg</fileName>rn" +
" <fileRef>cid:" + attachCid + "</fileRef>rn" +
" </uploadFileRequest>rn" +
" </soap:Body>rn" +
"</soap:Envelope>rn";
out.write(soapPart.getBytes("UTF-8"));
// 写入附件部分
File attachFile = new File(attachmentPath);
String attachHeader = "--" + boundary + "rn" +
"Content-Type: image/jpegrn" +
"Content-ID: <" + attachCid + ">rn" +
"Content-Transfer-Encoding: binaryrnrn";
out.write(attachHeader.getBytes("UTF-8"));
FileInputStream fis = new FileInputStream(attachFile);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
fis.close();
out.write("rn".getBytes());
// 写入结束分隔符
String endBoundary = "--" + boundary + "--rn";
out.write(endBoundary.getBytes("UTF-8"));
out.flush();
out.close();
// 获取响应状态码
int responseCode = conn.getResponseCode();
System.out.println("响应状态码:" + responseCode);
conn.disconnect();
}
}
SwA的优缺点
优点
- 无需对附件进行Base64编码,减少了数据体积和编解码的性能开销
- 支持任意类型的附件,不限制附件的格式和大小
- 基于标准SOAP和MIME规范,兼容性好,多数SOAP框架都提供了支持
缺点
- MIME多部分封装的解析逻辑相对复杂,部分轻量级框架支持不够完善
- 相比后续的MTOM(消息传输优化机制)规范,SwA的附件引用方式不够标准化,不同实现的兼容性可能存在问题
- 早期规范的安全性相关定义不够完善,需要额外配置安全策略
SwA的应用场景
SwA适合以下场景使用:
- Web服务需要同时传递业务参数和大尺寸二进制附件,比如文件上传、图片同步等场景
- 遗留系统已经基于SwA实现附件传输,需要保持兼容性的情况
- 对传输性能有一定要求,不适合使用Base64编码嵌入附件的场景
如果项目是新的开发需求,也可以考虑使用MTOM替代SwA,MTOM是更晚推出的优化规范,在附件引用、性能优化上更有优势,不过SwA作为经典方案,仍然在很多存量系统中被广泛使用。
SOAP_with_AttachmentsSwAWeb服务SOAP协议附件传输修改时间:2026-07-09 02:21:13