我正在尝试JsonObject通过HttpClientWeb API发布。我不太确定该怎么做,在示例代码中找不到太多。
JsonObject
HttpClient
这是我到目前为止的内容:
var myObject = (dynamic)new JsonObject(); myObject.Data = "some data"; myObject.Data2 = "some more data"; HttpClient httpClient = new HttpClient("myurl"); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = httpClient.Post("", ???);
我想我需要把我JsonObject当成演员,StreamContent但我对此很挂念。
StreamContent
使用的新版本HttpClient和不使用的WebApi软件包,它将是:
WebApi
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json"); var result = client.PostAsync(url, content).Result;
或者,如果您想要的话async:
async
var result = await client.PostAsync(url, content);