【发布时间】:2020-03-01 17:11:35
【问题描述】:
任务是发送HTTPweb 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)
我可以在哪里以及如何进行转换?
【问题讨论】:
-
您能否将
soapEnvelopeXml写入MemoryStream,然后在MemoryStream内容上使用Convert.ToBase64String(ToArray()或GetBuffer()plus @ 987654345@ -GetBuffer()超大) -
您需要在 XML 中添加一个包含 base64 字符串的“body”标签。
-
@jdweng 我希望将整个 SOAP 转换为 base64,而不仅仅是将其插入正文并粘贴。马克 我真的不明白你的回答。我试过了,但我无法将 xml 写入内存字符串?
-
一个请求需要 xml 标签。如果您想要整个 SOAP base64,那么您必须不将 mase64 作为 SOAP 发送,而是使用另一种方法,例如使用 TCP 发送数据。