1

HttpWebRequestコンソール アプリケーションで添付ファイルを送信しようとしています。これに関する理解できる支援を求めてインターネットを検索して精査した数日後、私はこのサイトからまともな解決策だと思うものを思いつきました

すべてを正しく行ったと思いますが、次のエラーが表示されます。

終了境界が検出される前に、マルチパート ストリームが終了しました。

質問:
受け取っ たエラーについて支援/ガイダンスを得たいと思っています。また、XML ドキュメントmultipartの実際のファイルを添付する際の支援も受けたいと思っています。byte[]

要件:

  1. 添付する必要があるデータ ファイルは、.xml である必要がある XML ファイルですMTOM Attachment。それを作るためにMtom、私の理解では、の要素のmessageEncoding 属性に値が必要であり、これがそのようにエンコードされることを確認する必要があります。<binding>app.config"Mtom"
  2. ビジネス要件 (厳密なもの) に基づいてbyte[]、コンテンツそのものではなく、ファイルの を送信する必要があります。

ウェブリクエスト方法

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

リクエストを送信する方法この私の投稿
に 基づいて、私は を使用して適切に圧縮していると思います。HttpWebRequestGZip

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--
4

0 に答える 0