【发布时间】:2018-09-19 11:22:03
【问题描述】:
我有一个调用应用程序,其代码如下(我无法更改此应用程序)
try
{
string request = string.Format("UniqueId={0}&MobileNumber={1}&UssdText={2}&Type={3}&AccountId={4}", "1", "2", "3", "4",
"5");
using (HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler())))
{
string url = "http://localhost/MocExternalEntityApis/MyUssd/Getdata3";
client.DefaultRequestHeaders.ExpectContinue = false;
StringContent content = new StringContent(request);
content.Headers.Clear();
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync(url, content).ConfigureAwait(false);
var readAsString = await response.Content.ReadAsStringAsync();
client.Dispose();
}
}
catch (Exception ex)
{
}
我的调用来的 web api 控制器总是有空对象
[HttpPost]
[ActionName("GetData3")]
public JsonResult<MyResponse> GetData3(MyInput obj)
{
if (obj != null)
{
Logger.DebugFormat("UniqueId:{0}, MobileNumber:{1}, UssdText:{2}, Type:{3}, AccountId:{4}",
obj.UniqueId, obj.MobileNumber, obj.UssdText, obj.Type, obj.AccountId);
if (obj.Type == "3")
{
Task.Factory.StartNew(async () =>
{
await ProcessCallbackHandlingofPinRespone(obj.UniqueId, obj.MobileNumber,
obj.UssdText);
});
}
else
{
Task.Factory.StartNew(async () =>
{
await ProcessCallbackHandlingOfNotification(obj.UniqueId, obj.MobileNumber,
obj.UssdText);
});
}
}
else
{
Logger.DebugFormat("Empty Object");
}
return Json(new MyResponse { Status = "OK" });
}
[Serializable]
public class MyInput
{
[JsonProperty(PropertyName = "UniqueId")]
public string UniqueId { get; set; }
[JsonProperty(PropertyName = "MobileNumber")]
public string MobileNumber { get; set; }
[JsonProperty(PropertyName = "UssdText")]
public string UssdText { get; set; }
[JsonProperty(PropertyName = "Type")]
public string Type { get; set; }
[JsonProperty(PropertyName = "AccountId")]
public string AccountId { get; set; }
}
我需要在 My web Api 中进行哪些更改才能使用数据。
对我的 api 的调用日志就像 要求:
Method: POST, RequestUri: 'http://localhost/MocExternalEntityApis/MyUssd/Getdata3', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Content-Type: application/json
}
UniqueId=1&MobileNumber=2&UssdText=3&Type=4&AccountId=5
【问题讨论】:
-
刚刚从stackoverflow.com/questions/40407884/…找到了一个可能的解决方案
标签: c# asp.net-web-api .net-4.5