通过SOAP HttpWebRequest发送附件

时间:2016-02-04 21:00:46

标签: c# soap httpwebrequest soap-client irs

我正在尝试通过控制台应用程序中的HttpWebRequest发送附件。经过几天的搜索和搜索互联网寻求一些可以理解的帮助,我提出了我认为来自this site的正确解决方案

虽然我认为我已正确完成所有操作,但我收到以下错误:

  

在遇到终止边界之前,Multipart流已结束。

问题:
我希望得到一些关于我收到的multipart错误的帮助/指导,以及协助附加XML文档的实际byte[]

要求:

  1. 需要附加的数据文件是XML文件 应该是MTOM Attachment。为了使它Mtom,我的 理解是我需要确保messageEncoding <binding>app.config元素的属性应具有 值"Mtom",这将按此编码。
  2. 根据业务要求(严格要求),我需要发送文件的byte[],而不仅仅是内容本身。
  3. 网络请求方法

    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.");
        }
    }
    

    提交请求的方法
    根据我的this post,我相信我正在使用HttpWebRequest正确压缩GZip

    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 Body Snippet Element
    通过使用Fiddler,我能够看到请求的实际情况。对我来说,似乎附件实际上是作为XML而不是(我认为会是)byte[]附加到请求中。

    此后应该是附件的byte[]。目前,正在显示完整的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--
    

0 个答案:

没有答案