设置Restsharp参数

时间:2016-02-18 15:15:38

标签: c# http-post restsharp

我一直围着这个圈子走了一圈。我正在尝试使用 Restsharp 发帖。问题是我得到一个关于application/xml的错误作为唯一允许的格式或prolog中不允许的内容。在下面的示例中,如果我使用AddBody(),那么我将收到有关application/xml的错误(在请求对象的监视中,我看到正文填充为text/xml),如果我这样做的话AddParameter()ParameterType.RequestBody然后我收到有关prolog中内容的错误。我很难控制,甚至正是在帖子中发送的内容。

var client = new RestClient("https://portfoliomanager.energystar.gov/wstest/");
var request = new RestRequest("{token}", Method.POST);

meterConsumptionType meteruse = new meterConsumptionType();

account newuser = new account();

// Populating object
request.AddHeader("Content-Type", "application/xml");

request.Parameters.Clear();
request.AddParameter("application/xml", newuser, ParameterType.RequestBody);

//request.AddBody(newuser);
request.AddUrlSegment("token", "account");

request.Parameters[1].ContentType = "application/xml";

IRestResponse response = client.Execute(request);

1 个答案:

答案 0 :(得分:0)

添加此行以更改序列化程序: request.XmlSerializer.ContentType =“application / xml”;