(System.Net.Http)DeleteAsync
で関数を使用していて、コンテンツを取得すると、常に返されます。HttpClient
Content.ReadAsStringAsync()
null
GET
、POST
and - で同じことを試しましたが、PUT
常に何らかの結果が返されます。
これが私のコードです:
HttpClient _client = new HttpClient();
_client.BaseAddress = new Uri("http://httpbin.org/");
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = _client.DeleteAsync("/delete").Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
いつもnull
返ってきます。
ただし、これはすべて機能します。
得る:
HttpResponseMessage response = _client.GetAsync("/get").Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
役職:
HttpResponseMessage response = _client.PostAsync("/post", new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")).Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
置く:
HttpResponseMessage response = _client.PutAsync("/put", new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")).Result;
string res = await response.Content.ReadAsStringAsync();
return await JsonConvert.DeserializeObjectAsync<T>(res);
しかしDeleteAsync()
、ReadAsStringAsync()
常に私を返しますnull
。
RFCによると、ステータス コード 200 OK を返すときにボディを返す必要があります。