我正在尝试实现this rest api:

public async Task<bool> AddTimetracking(Issue issue, int spentTime)
{
    // POST /rest/issue/{issue}/timetracking/workitem
    var workItem = new WorkItem(spentTime, DateTime.Now);
    var httpContent = new StringContent(workItem.XDocument.ToString());
    var requestUri = string.Format("{0}{1}issue/{2}/timetracking/workitem", url, YoutrackRestUrl, issue.Id);
    var respone = await httpClient.PostAsync(requestUri, httpContent);
    if (!respone.IsSuccessStatusCode)
    {
        throw new InvalidUriException(string.Format("Invalid uri: {0}", requestUri));
    }

    return respone.IsSuccessStatusCode;
}
workItem.XDocument包含以下元素:

<workItem>
  <date>1408566000</date>
  <duration>40</duration>
  <desciption>test</desciption>
</workItem>

我从API中收到一个错误:Unsupported Media Type
我真的不知道如何解决此问题,非常感谢您的帮助。如何使用HttpClient通过HTTP POST URI封送XML文件?

最佳答案

您可能想要在StringContent中设置mediaType,如下所示:

var httpContent = new StringContent(workItem.XDocument.ToString(), Encoding.UTF8, "application/xml");

10-07 12:31