将 XmlDocument 上传到端点

Upload XmlDocument to endpoint

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

我们正在谈论的终点是这个(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, 0, 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, 0, 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;  
                
}