我开始通过创建Web API和客户端来使用Web Api,所有工作正常,直到我从GetAsync()
切换到PostAsync()
,现在每次都得到404响应,为什么会这样是?
服务器端代码
[HttpPost]
[Route("users/verifyLoginCredentials/{username}/{password}")]
public IHttpActionResult VerifyLoginCredentials(string username, string password)
{
var verificationResult = Models.User.VerifyLoginCredentials(username, password);
if (verificationResult)
{
return Ok("Login Successful");
}
return BadRequest("Login Failed");
}
.Net 4.5客户端代码
static async Task<Uri> verifyLoginCredentials(string username, string password)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:4685/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var value = new Dictionary<string, string>
{
{ "username", "hello" },
{ "password", "world" }
};
var content = new FormUrlEncodedContent(value);
var result = await client.PostAsync("users/verifyLoginCredentials/", content);
string resultContent = await result.Content.ReadAsStringAsync();
result.EnsureSuccessStatusCode();
// return URI of the created resource.
return result.Headers.Location;
}