C#的HttpClient PostAsync变成204到404 [英] C# HttpClient PostAsync turns 204 into 404
问题描述
考虑到这种服务的WebAPI:
[ActionName(KillPerson)]
[HttpPost]
公共无效KillPerson([FromBody]长ID)
{
//杀死
}
这HttpClient的PostAsync电话:
VAR的HttpClient =新的HttpClient {BaseAddress =新的URI(ClientConfiguration.ApiUrl)};
httpClient.DefaultRequestHeaders.Accept.Add(新MediaTypeWithQualityHeaderValue(应用/ JSON));
VAR serializerSettings =新JsonSerializerSettings
{
preserveReferencesHandling = preserveReferencesHandling.Objects,
格式化= Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};
VAR serializedParameter = JsonConvert.SerializeObject(参数,serializerSettings);
VAR httpContent =新的StringContent(serializedParameter,Encoding.UTF8,应用/ JSON);
VAR响应=等待httpClient.PostAsync(的serviceUrl,httpContent).ConfigureAwait(假);
response.EnsureSuccessStatus code();
我希望response.EnsureSuccessStatus code();到succede但它抛出一个404来代替。
有趣的是,提琴手告诉配有该服务的WebAPI正在恢复204如预期,当我调试它KillPerson运行没有问题。
更新:
我已经确定,当客户端code是一个PCL或Silverlight 5项目中这只是发生。完全相同的code将使预期的204,如果我重复它在Windows窗体应用程序。如果我点了Windows窗体应用包含在PCL它给我的404客户端再次code。
UPDATE2:
这样就解决了这个问题(尽管它困扰我没有尽头,我应该需要做的话):
[ActionName(KillPerson)]
[HttpPost]
公众的Htt presponseMessage KillPerson([FromBody]长ID)
{
返回this.Request.CreateResponse(的HTTPStatus code.OK);
}
这重新引入404(提琴手还在说204和非Silverlight客户端运行正常)
[ActionName(KillPerson)]
[HttpPost]
公众的Htt presponseMessage KillPerson([FromBody]长ID)
{
返回this.Request.CreateResponse(的HTTPStatus code.NoContent);
}
更新3(解决):
终于想通了这一点。看来你使用任何浏览器或客户端HTTP在Silverlight处理的选择。当使用浏览器HTTP处理很多东西是不支持 - 包括各种应对codeS和头。
调用HttpClient的固定前加上几行:
WebRequest.Register preFIX(HTTP://,WebRequestCreator.ClientHttp);
WebRequest.Register preFIX(https://开头,WebRequestCreator.ClientHttp);
终于想通了这一点。看来你使用任何浏览器或客户端HTTP在Silverlight处理的选择。当使用浏览器HTTP处理很多东西是不支持 - 包括各种应对codeS和头。调用HttpClient的固定前加上几行:
WebRequest.Register preFIX(HTTP://,WebRequestCreator.ClientHttp);
WebRequest.Register preFIX(https://开头,WebRequestCreator.ClientHttp);
Given this WebApi service:
[ActionName("KillPerson")]
[HttpPost]
public void KillPerson([FromBody] long id)
{
// Kill
}
And this HttpClient PostAsync call:
var httpClient = new HttpClient { BaseAddress = new Uri(ClientConfiguration.ApiUrl) };
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var serializerSettings = new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};
var serializedParameter = JsonConvert.SerializeObject(parameter, serializerSettings);
var httpContent = new StringContent(serializedParameter, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(serviceUrl, httpContent).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
I would expect response.EnsureSuccessStatusCode(); to succede but it throws a 404 instead. The funny thing is that fiddler tells med that the webapi service is returning 204 as expected and when I debug it the KillPerson runs without issue.
Update: I Have determined that this only happens when the client code is within a PCL or Silverlight 5 project. The exact same code will give the expected 204 if I duplicate it in a Windows forms application. If i point the Windows Forms app to client code contained in a PCL it gives me the 404 Again.
Update2: This resolves the issue (though it bothers me no end that I should need to do it):
[ActionName("KillPerson")]
[HttpPost]
public HttpResponseMessage KillPerson([FromBody] long id)
{
return this.Request.CreateResponse(HttpStatusCode.OK);
}
This reintroduces the 404 (fiddler still says 204 and non-silverlight client runs fine)
[ActionName("KillPerson")]
[HttpPost]
public HttpResponseMessage KillPerson([FromBody] long id)
{
return this.Request.CreateResponse(HttpStatusCode.NoContent);
}
Update 3 (resolved): Finally figured this out. Seems you get a choice of using either browser or client HTTP handling in Silverlight. When using browser HTTP handling a lot of stuff is unsupported - including various response codes and headers. Adding these lines before calling HttpClient fixed it:
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
Finally figured this out. Seems you get a choice of using either browser or client HTTP handling in Silverlight. When using browser HTTP handling a lot of stuff is unsupported - including various response codes and headers. Adding these lines before calling HttpClient fixed it:
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
这篇关于C#的HttpClient PostAsync变成204到404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!