wp7应用中使用RestSharp提交到使用json格式的 http post api,折腾了半天发现,用RestRequest.AddParameter方法添加类型为body的参数不可行:
RequestBody
If this parameter is set, it’s value will be sent as the body of the request. The name of the Parameter is ignored, and so are additional RequestBody Parameters – only 1 is accepted.
https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest
使用匿名类调用RestRequest.AddBody方法比较方便,例如:
request.AddBody(new { p1= "p1value", p2 = new { p2a = "p2avalue", p2b= "p2bvalue" } });
使用了匿名类,则需要添加如下到AssemblyInfo.cs:
//give access to RestSharp, Newtonsoft to access anonymous type created in this assembly [assembly: InternalsVisibleTo("RestSharp.WindowsPhone")] [assembly: InternalsVisibleTo("Newtonsoft.Json")]参加前文