如何将XML编码为base64,然后保存到流中?
How to encode XML to base64 and then save it to stream?
任务是发送 HTTP
web request
以及必须转换为 base64
的 Soap 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)
在哪里以及如何进行转换?
将您的 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
写入使用 StringWriter
的 TextWriter
。完成此操作后,您必须先 encode
,然后将其转换为 byte array
。这只是将其写入流的问题。您将数组、0 偏移量和字节数组的长度作为参数。
任务是发送 HTTP
web request
以及必须转换为 base64
的 Soap 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)
在哪里以及如何进行转换?
将您的 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
写入使用 StringWriter
的 TextWriter
。完成此操作后,您必须先 encode
,然后将其转换为 byte array
。这只是将其写入流的问题。您将数组、0 偏移量和字节数组的长度作为参数。