[英]C# Post XML file using RestSharp
我已經編寫了一種將消息發布到uri的方法。
public string RestClientPost(string uri, string message = null)
{
var client = new RestClient(uri);
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "text/xml");
if (!string.IsNullOrEmpty(message))
request.AddParameter(message, ParameterType.RequestBody);
var result = "";
var response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
result = response.Content;
Console.WriteLine(result);
}
else
{
result = response.StatusCode.ToString();
}
return result;
}
並且下面的代碼用於上面的方法發布。
public void test123()
{
string uri = "myuri"; //private uri, cannot expose.
var file= System.IO.File.ReadAllText(Path.Combine(Settings.EnvValPath, "RestClientXML", "test.XML"));
var content = new RestClientServices().RestClientPost(uri, file);
}
但是,它返回“不支持的媒體類型”。
我的test.XML的內容是
<customer>
<customerName>test</customerName >
<customerStatus>OK</customerStatus >
</customer>
並使用適用於Google Chrome的Advanced Rest Client插件,我可以發布它並返回所需的字符串。 有什么不對?? 我在Advanced Rest Client中將“ content-type”設置為“ text / xml”。
發生這種情況是因為標頭“ Accept”用於指定返回對象的類型。 在這種情況下,可變內容的值不是要發送的內容的類型。 指定要發送的內容類型:“ Content-Type:應用程序/ xml”。
如果POST請求的返回類型是媒體文件,則可以使用'image / png'或'image / jpeg'。 您可以使用多個接受標頭值,例如:“ application / xml,application / xhtml + xml和image / png ”。 例如,您可以使用Fiddler調試HTTP流量-這是Web開發人員的好工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.