【问题标题】:How to encode XML to base64 and then save it to stream?如何将 XML 编码为 base64,然后将其保存到流中?
【发布时间】:2020-03-01 17:11:35
【问题描述】:

任务是发送HTTPweb request 以及必须转换为base64Soap Envelope

不幸的是,我无法将Envelope 转换为base64,因为我的方法InsertSoapEnvelopeIntoWebRequest 使用XML 并将其保存到stream,如下所示。我需要遵循这个结构才能使请求生效。

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope(attachmentName);
    HttpWebRequest webRequest = CreateWebRequest(url, action, attachmentName);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

    private static XmlDocument CreateSoapEnvelope(string attachmentName)
    {
        //Convert name to base64
        var fileNameEncoded = EncodeStringToBase64(attachmentName);
        var authorizationIdEncoded = EncodeStringToBase64("C61582-B73K47EJ54");
        var authorizationKeyEncoded = EncodeStringToBase64("TWTXBP-HNEZ9J-74EV8Z-QM5J9T");
        XmlDocument soapEnvelopeDocument = new XmlDocument();
        soapEnvelopeDocument.LoadXml($@"<?xml version=""1.0"" encoding=""UTF-8""?><SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:ns4=""https://efaktura.bg/soap/""><SOAP-ENV:Body><ns4:uploadFile><ns4:authorizationId>{authorizationIdEncoded}</ns4:authorizationId><ns4:authorizationKey>{authorizationKeyEncoded}</ns4:authorizationKey><ns4:fileName>{fileNameEncoded}</ns4:fileName></ns4:uploadFile></SOAP-ENV:Body></SOAP-ENV:Envelope>");
    //            doc.LoadXml(soapRequest.ToString());
        return soapEnvelopeDocument;
    }
    public static string EncodeStringToBase64(string plainTextBytes)
    {
        var plainText = System.Text.Encoding.UTF8.GetBytes(plainTextBytes);

        return System.Convert.ToBase64String(plainText);
    }

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }

我尝试将 XML 转换为 String 但在 CreateSoapEnvelope 我有一个方法 LoadXML 如果它是 base64 ,该方法无法读取它,因为它不是 XML 格式.

如果我在InsertSoapEnvelopeIntoWebRequest 中进行转换,那么我将无法使用soapEnvelopeXml.Save(stream)

我可以在哪里以及如何进行转换?

【问题讨论】:

  • 您能否将soapEnvelopeXml 写入MemoryStream,然后在MemoryStream 内容上使用Convert.ToBase64StringToArray()GetBuffer() plus @ 987654345@ - GetBuffer() 超大)
  • 您需要在 XML 中添加一个包含 base64 字符串的“body”标签。
  • @jdweng 我希望将整个 SOAP 转换为 base64,而不仅仅是将其插入正文并粘贴。马克 我真的不明白你的回答。我试过了,但我无法将 xml 写入内存字符串?
  • 一个请求需要 xml 标签。如果您想要整个 SOAP base64,那么您必须不将 mase64 作为 SOAP 发送,而是使用另一种方法,例如使用 TCP 发送数据。

标签: c# xml soap encode


【解决方案1】:

将您的 InsertSoapEnvelope 方法更改为:

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            StringWriter sw = new StringWriter();
            XmlTextWriter tx = new XmlTextWriter(sw);
            soapEnvelopeXml.WriteTo(tx);

            string str = sw.ToString();

            Console.WriteLine(soapEnvelopeXml.ToString());
            ASCIIEncoding Encode = new ASCIIEncoding();
            var arr = Encode.GetBytes(EncodeStringToBase64(str));
            stream.Write(arr, 0, arr.Length);
        }
    }

如果你 Console.Writeline XML.ToString() 它会给你参考。想法是将XML 写入使用StringWriterTextWriter。完成此操作后,您必须先将其encode,然后将其转换为byte array。这只是将其写入流的问题。您将数组、0 偏移量和字节数组的长度作为参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2016-09-18
    相关资源
    最近更新 更多