使用HttpClient时,Same Rest Api返回内部服务器错误

时间:2016-05-25 10:32:17

标签: c# rest httpclient

我正在向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();
}

第二个片段中我可能缺少什么?

0 个答案:

没有答案