C# 如何将包含XML的长字符串发布到Web API

C# 如何将包含XML的长字符串发布到Web API,c#,xml,httpclient,asp.net-web-api,C#,Xml,Httpclient,Asp.net Web Api,我试图将一个长字符串(包含XML)发布到我的Web API控制器,但失败得很惨 如果TextAsXml很短,那么下面的方法可以工作,但是当TextAsXml很长时,它会失败“无效URI:URI字符串太长”,这是可以理解的 // Client code using (var client = new HttpClient()) { var requestUri = "http://localhost:49528/api/some"; var content = new FormUrlEnc

我试图将一个长字符串(包含XML)发布到我的Web API控制器,但失败得很惨

如果TextAsXml很短,那么下面的方法可以工作,但是当TextAsXml很长时,它会失败“无效URI:URI字符串太长”,这是可以理解的

// Client code
using (var client = new HttpClient())
{
  var requestUri = "http://localhost:49528/api/some";
  var content = new FormUrlEncodedContent(new[] 
  {
    new KeyValuePair<string, string>("Author", "John Doe"),
    new KeyValuePair<string, string>("TextAsXml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>")
  });

  var response = client.PostAsync(requestUri, content).Result;
  response.EnsureSuccessStatusCode();
}

// Controller code
public HttpResponseMessage Post(SomeModel someModel)
{
  // ...
  return Request.CreateResponse(HttpStatusCode.OK);
}

public class SomeModel
{
  public string Author { get; set; }
  public string TextAsXml { get; set; }
}

将XML作为内容发送而不使用Uri编码。

我已经了解到的部分:-)但是如何发送呢?正如您在我的示例中看到的,该示例导致500个内部服务器错误,我使用MultipartFormDataContent调用PostAsync,但运气不佳。
// This results in 500 Internal server error.
using (var client = new HttpClient())
{
  var requestUri = "http://localhost:49528/api/some";
  var textAsXml = File.ReadAllText("Note.xml");
  var content = new MultipartFormDataContent();
  content.Add(new StringContent("John Doe"), "Author");
  content.Add(new StringContent(textAsXml), "TextAsXml");

  var response = client.PostAsync(requestUri, content).Result;
  response.EnsureSuccessStatusCode();
}