我正在向Rest网址发送PUT请求。如果我执行以下操作,它将起作用:
var client = new HttpClient()
{
BaseAddress = new Uri(@"https://myip:myport/"),
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, uri))
{
request.Content = new StringContent(JsonConvert.SerializeObject(myobject), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
}
但是,如果我执行以下操作,我会得到500 Internal Server Error
:
var client = new HttpClient()
{
BaseAddress = new Uri(@"https://myip:myport/"),
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage response = client.PutAsJsonAsync(uri, myobject).Result)
{
var r = response.EnsureSuccessStatusCode();
}
第二个片段中我可能缺少什么?