无法将字符串转换为system.Net.HttpContent

时间:2019-02-05 07:52:03

标签: c# json asp.net-web-api dotnet-httpclient

我正在尝试从服务中获取数据:

public async Task<IHttpActionResult> ValidateSesion()
{
    var values = new Dictionary<string, string>{
        { "productId", "1" },
        { "productKey", "Abc6666" },
        { "userName", "OPPO" },
    };
    var json = JsonConvert.SerializeObject(values, Formatting.Indented);
    // var content = new FormUrlEncodedContent(json);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var response = await client.PostAsync("http://172.116.12.172:8014/iadmin/validate", json);
    var responseString = await response.Content.ReadAsStringAsync();
    return Ok(responseString);
}

如果我拨打任何PostMan电话,如下所示,我得到的数据如下:

{
    "productId": "1",
    "productKey": "Abc6666" 
}

2 个答案:

答案 0 :(得分:1)

将您的退货声明更改为

return Content(responseString);

答案 1 :(得分:1)

您不能发布原始字符串,必须将其包装在StringContent中: String.format("%.1f", floatValue);

这应该可以解决问题:

new StringContent(json);