C# 通过SOAP HttpWebRequest发送附件
我试图通过控制台应用程序中的C# 通过SOAP HttpWebRequest发送附件,c#,soap,httpwebrequest,soap-client,irs,C#,Soap,Httpwebrequest,Soap Client,Irs,我试图通过控制台应用程序中的HttpWebRequest发送附件。经过几天的搜索和搜索,我在互联网上找到了一些可以理解的帮助,我从中找到了一个我认为是不错的解决方案 虽然我认为我做的一切都是正确的,但我收到了以下错误: 多部分流在遇到终止边界之前结束 问题: 我希望得到一些关于我收到的multipart错误的帮助/指导,以及附加XML文档的实际byte[]的帮助 要求: 需要附加的数据文件是一个XML文件 应为MTOM附件。为了使其成为Mtom,我的 理解是我需要确保messageEncodin
HttpWebRequest
发送附件。经过几天的搜索和搜索,我在互联网上找到了一些可以理解的帮助,我从中找到了一个我认为是不错的解决方案
虽然我认为我做的一切都是正确的,但我收到了以下错误:
多部分流在遇到终止边界之前结束
问题:我希望得到一些关于我收到的
multipart
错误的帮助/指导,以及附加XML文档的实际byte[]
的帮助
要求:
MTOM附件
。为了使其成为Mtom,我的
理解是我需要确保messageEncoding
app.config
中
元素的属性应具有
值“Mtom”
,并将按此编码字节[]
,而不仅仅是内容本身private static HttpWebRequest CreateWebRequest(SoapAction action)
{
// Retrieve URL from Endpoint in the app.Config based on the action passed into the
// method.
string url = GetUrlAddress(action);
if (url != null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");
request.Headers.Add("Content-Transfer-Encoding", "8bit");
request.Headers.Add("SOAPAction", action.ToString());
request.Method = "POST";
request.Headers.Add("MIME-Version", "1.0");
request.ContentType = "multipart/form-data; type=\"application/xop+xml;\" boundary=\"" + BoundaryMarker + "\"";
request.ClientCertificates.Add(SecurityCertificate.CertificateObject);
ServicePointManager.Expect100Continue = false;
return request;
}
else
{
throw new NullReferenceException("Address for Endpoint could not be resolved.");
}
}
提交请求的方法基于我的经验,我相信我正在使用
GZip
适当地压缩HttpWebRequest
private static void SubmitRequest(HttpWebRequest request, XDocument soapXml, byte[] formXmlBytes, FileInfo fileToUpload)
{
using (Stream requestStream = request.GetRequestStream())
{
using (GZipStream gz = new GZipStream(requestStream, CompressionMode.Compress, false))
{
soapXml.Save(gz);
WriteToStream(gz, formXmlBytes, fileToUpload.Name);
}
}
}
用于将MIME信息和附件写入流的方法
public static void WriteToStream(Stream stream, byte[] formData, string fileName)
{
// Write a new line to the stream.
byte[] newLineBytes = Encoding.UTF8.GetBytes("\r\n");
stream.Write(newLineBytes, 0, newLineBytes.Length);
// Write the header to the stream.
string header = String.Format(HeaderTemplate, BoundaryMarker, fileName, RequestContentID);
byte[] headerBytes = Encoding.UTF8.GetBytes(header);
stream.Write(headerBytes, 0, headerBytes.Length);
// Write a new line to the stream.
stream.Write(newLineBytes, 0, newLineBytes.Length);
// Write the formData to the stream.
stream.Write(formData, 0, formData.Length);
// Write a new line to the stream.
stream.Write(newLineBytes, 0, newLineBytes.Length);
// Write the footer to the stream.
byte[] boundaryFooterBytes = Encoding.UTF8.GetBytes("--" + BoundaryMarker + "--");
stream.Write(boundaryFooterBytes, 0, boundaryFooterBytes.Length);
}
Soap正文片段元素通过使用
Fiddler
,我可以看到请求的实际外观。在我看来,附件实际上是以XML的形式附加到请求中的,而不是(我认为是)一个字节[]
之后应该是附件的字节[]
。目前,正在显示完整的XML文档
Accept-Encoding: gzip, deflate
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
Content-Type: multipart/form-data; type="application/xop+xml;" boundary="--b73acdd180274cab985e4d697bfde428"
Content-Length: 582081
Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="..." ...>
...
<soapenv:Body>
<urn:Request version="1.0">
<urn:FileAttachment>cid:b73acdd180274cab985e4d697bfde428</urn:FileAttachment>
</urn:Request>
</soapenv:Body>
</soapenv:Envelope>
----b73acdd180274cab985e4d697bfde428
Content-Disposition: attachment; filename="test.xml"
Content-Type: text/xml
Content-Id: b73acdd180274cab985e4d697bfde428
<XML OF ATTACHMENT>
----b73acdd180274cab985e4d697bfde428--
接受编码:gzip,deflate
内容传输编码:8比特
MIME版本:1.0
内容类型:多部分/表单数据;type=“application/xop+xml;”boundary=“--b73acdd180274cab985e4d697bfde428”
内容长度:582081
连接:保持活力
...
cid:b73acdd180274cab985e4d697bfde428
----b73acdd180274cab985e4d697bfde428
内容处置:附件;filename=“test.xml”
内容类型:text/xml
内容Id:b73acdd180274cab985e4d697bfde428
----b73acdd180274cab985e4d697bfde428--
嘿,罗斯。看来我们都在同一条轨道上。你能用C代码发送MTOM附件吗?如果是的话,你能分享你的代码片段或其他例子吗?今天我有几个方法可以尝试。如果我有什么事情可以做,我一定会让你(和社区)知道的。谢谢Russ。我也在尝试同样的方法,如果成功的话,我会告诉你的。嗨,罗斯,还没有运气。你可以通过形成MTOM附件来使用HTTPRequest打电话吗?@Oxygen不,我被错误消息卡住了消息格式不正确和/或无法解释。嘿,Russ。看来我们都在同一条跟踪线上。你能用C代码发送MTOM附件吗?如果是的话,你能分享你的代码片段或其他例子吗?今天我有几个方法可以尝试。如果我有什么事情可以做,我一定会让你(和社区)知道的。谢谢Russ。我也在尝试同样的方法,如果成功的话,我会告诉你的。嗨,罗斯,还没有运气。您是否能够通过形成MTOM附件来使用HTTPRequest进行呼叫?@Oxygen否,我被错误消息卡住了消息格式不正确和/或无法解释。