如何发送XML内容与HttpClient.PostAsync? [英] How to send XML content with HttpClient.PostAsync?
本文介绍了如何发送XML内容与HttpClient.PostAsync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图完成这个REST API :
公共异步任务<布尔> AddTimetracking(发行问题,诠释spentTime)
{
// POST /休息/问题/ {}问题/时间跟踪/工作项目
变种的工作项目=新工作项目(spentTime,DateTime.Now);
变种httpContent =新的StringContent(workItem.XDocument.ToString());
VAR requestUri =的String.Format({0} {1}问题/ {2} /时间跟踪/工作项,网址,YoutrackRestUrl,issue.Id);
VAR输入反应=等待httpClient.PostAsync(requestUri,httpContent); (!respone.IsSuccessStatusCode)如果
{
抛出新InvalidUriException(的String.Format(无效的URI:{0},requestUri));
}
返回respone.IsSuccessStatusCode;
}
workItem.XDocument
包含以下元素:
<预类=郎咸平的XML prettyprint-覆盖>
<&工作项目GT;
<日期和GT; 1408566000< /日期和GT;
<持续时间GT; 40℃/持续时间GT;
<应将描述>测试与LT; /应将描述>
< /工作项目>
我从API得到一个错误:不支持的媒体类型
我真的不知道如何解决这个问题,帮助是极大的赞赏。我如何马歇尔的通过HTTP POST URI的XML文件,并使用的HttpClient
?
解决方案
您可能需要设置的MediaType中的StringContent象下面这样:
VAR httpContent =新的StringContent(workItem.XDocument.ToString(),Encoding.UTF8,应用程序/ XML);
I am trying to fulfill 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
contains the following elements:
<workItem>
<date>1408566000</date>
<duration>40</duration>
<desciption>test</desciption>
</workItem>
I am getting an error from the API: Unsupported Media Type
I really have no idea how to resolve this, help is greatly appreciated. How do I marshall an XML file via a HTTP POST URI, using HttpClient
?
解决方案
You might want to set the mediaType in StringContent like below:
var httpContent = new StringContent(workItem.XDocument.ToString(), Encoding.UTF8, "application/xml");
这篇关于如何发送XML内容与HttpClient.PostAsync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文