微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 XmlDocument 上传到端点

如何解决将 XmlDocument 上传到端点

我正在尝试将 XmlDocument 上传到端点,但我一定是做错了什么,因为它返回“错误请求”,这是我尝试过的:

我们正在谈论的 endpoing 是这个 (Validar Semilla): link to the swagger
如果我在 swagger 测试中手动上传文件,它会起作用,所以我的 xml 状态良好。

片段 1:

var httpClient = new HttpClient();
var someXmlString = MyXMLDoc.InnerXml;
var stringContent = new StringContent(someXmlString,Encoding.UTF8,"multipart/form-data");
var respone = await httpClient.PutAsync("/someurl",stringContent);

片段 2:

public static string postXMLData(string destinationUrl,string requestXml)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            request.ContentType = "multipart/form-data; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes,bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                return responseStr;
            }
            return null;
        }

示例 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<SemillaModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <valor>lWVzKj0dStSuFkR3nXx2mrf2R2q9J+x6JVuPk8jmx0Bwr8DUmI1A9+eld/lot8MNNhC17k8fLeQLGKXW2naiLXXJ41rf0GNS6vlz0CGvuMaCDKElzpnR4NNBBo6XRNcoCEEaA+3H0kBItG1W+bU0pA==</valor>
  <fecha>2021-07-10T10:46:28.4448582-04:00</fecha>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
        <DigestValue>VBFrBAoKPWRlOQO6RgVkY/3Jh6nNERnG2uGK+thFd24=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>ValuesIDoNotFeelSafeSharing</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>ValuesIDoNotFeelSafeSharing</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</SemillaModel>

我是 XML 的新手,我做错了什么?有什么可以帮助我的文章吗?

解决方法

也许您应该尝试验证xml结构,您可以在线进行:https://codebeautify.org/xmlvalidator

,

您需要传递文件内容,而不是字符串内容。尝试如下操作。

using (var content = new MultipartFormDataContent())  
{  
    byte[] Bytes = new byte[xmlfile.InputStream.Length + 1];  
    file.InputStream.Read(Bytes,Bytes.Length);  
    var fileContent = new ByteArrayContent(Bytes);  
    fileContent.Headers.ContentDisposition =
    new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = file.FileName };  

   content.Add(fileContent); 

   var requestUri = "http://{api url} ";  
   var result = client.PostAsync(requestUri,content).Result;  
                
} 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。