Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在尝试将此包含附件的多部分SOAP请求发送到web服务_C#_.net_Soap_Webrequest_Mtom - Fatal编程技术网

C# 正在尝试将此包含附件的多部分SOAP请求发送到web服务

C# 正在尝试将此包含附件的多部分SOAP请求发送到web服务,c#,.net,soap,webrequest,mtom,C#,.net,Soap,Webrequest,Mtom,我试图找到一个基于SOAP的MTOM服务,它接受多部分附件。下面是如何从C#创建请求: 您使用的是http,所以应该使用Base 64字符串,而不是UTF8。@jdweng我不能假设服务器支持Base 64,我只知道它支持二进制八位字节流。对我来说,它有点像一个黑盒子。根据规范,http附件总是以64个字符串为基数。服务器必须支持base 64。它实际上与GZIP相同。@jdweng这是一个SOAP+MTOM服务器。它的工作原理不同,就像这样->二进制数据可以放置到单独的MIME部分,而无需ba

我试图找到一个基于SOAP的MTOM服务,它接受多部分附件。下面是如何从C#创建请求:


您使用的是http,所以应该使用Base 64字符串,而不是UTF8。@jdweng我不能假设服务器支持Base 64,我只知道它支持二进制八位字节流。对我来说,它有点像一个黑盒子。根据规范,http附件总是以64个字符串为基数。服务器必须支持base 64。它实际上与GZIP相同。@jdweng这是一个SOAP+MTOM服务器。它的工作原理不同,就像这样->二进制数据可以放置到单独的MIME部分,而无需base-64编码。它只支持http 1.1(而不是流http 1.0)的分块模式。看见
var request = (HttpWebRequest)WebRequest.Create("...skipping URL...");
request.ContentType = @"multipart/related;charset=""UTF-8"";type=""application/xop+xml"";start=""<http://tempuri.org/0>"";boundary=""uuid:f276e990-75d0-4b5d-a0fd-e00a096e30ce+id=1"";start-info=""application/soap+xml""";
request.Method = "POST";
request.Timeout = 30000;
request.Headers.Add("MIME-Version", "1.0");
request.Headers.Add("Accept-Encoding", "gzip, deflate");

var postData = @"--uuid:f276e990-75d0-4b5d-a0fd-e00a096e30ce+id=1
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type=""text/xml""

[Skipping the SOAP envelope that goes here...]

--uuid:f276e990-75d0-4b5d-a0fd-e00a096e30ce+id=1
Content-ID: <http://tempuri.org/1/636875796573424479>
Content-Transfer-Encoding: binary
Content-Type: application/octet-stream

" + Encoding.UTF8.GetString(File.ReadAllBytes(@"C:\Users\User\Desktop\Sample.pdf")) + @"

--uuid:f276e990-75d0-4b5d-a0fd-e00a096e30ce+id=1--";

using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
    writer.Write(postData);
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
using (var attachment = new FileStream(@"C:\Users\User\Desktop\Sample.pdf", FileMode.Open, FileAccess.Read))
{
    writer.Write(postDataPrefix); // Add everything up to the start of the attachment binary. 
    writer.Flush();
    stream.Flush();
    attachment.CopyTo(stream); // Add the actual attachment binary.
    writer.Flush();
    stream.Flush();
    writer.Write(postDataSuffix); // Add everything after the attachment binary.
    writer.Flush();
    stream.Flush();
}