首页 > 解决方案 > c# RestClient 在发送以 XML 作为参数的 Post 请求时出错

问题描述

我正在尝试使用 RestClient 将 HttpResults 更新为 ALM Octane,结果是 XML 不遵守 XSD。

当我使用邮递员时,它运行良好。但是 C# 代码失败了。

XML - >

<test_result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <release_ref id="123" />
  <product_areas>
    <product_area_ref id="233" />
  </product_areas>
  <test_fields>
    <test_field type="Framework" value="TestNG" />
    <test_field type="Testing_Tool_Type" value="Selenium" />
    <test_field type="Test_Level" value="System Test" />
    <test_field type="Test_Type" value="Regression" />
  </test_fields>
  <environment>
    <taxonomy type="AUT Env" value="Production" />
  </environment>
  <test_runs>
    <test_run class="Login" duration="741" package="TestProject" status="Passed" name="Validate Login Page" />
  </test_runs>
</test_result>

API 链接 -> API 链接

我目前正在使用的代码。

StreamReader reader = new StreamReader(path);
string ret = reader.ReadToEnd();
reader.Close();

String URI = "/api/shared_spaces/" + sspace + "/workspaces/" + wspace + "/test-results";

var client = new RestClient(octaneBaseUrl);
client.Authenticator = new HttpBasicAuthenticator(userId, pwd);

var request = new RestRequest(URI, Method.POST);
request.AddHeader("Content-Type", "application/xml");
request.AddParameter("xmlBody", ret);

IRestResponse response = client.Post(request);
string res = response.Content;

在发送 POST 之前,我应该在 XML 中包含 XML 标记吗?

标签: c#xmlserializationhttpclientrest-client

解决方案


推荐阅读