简体   繁体   中英

HttpClient not returning NotFound content from WebAPI

Web API Code:

        // POST api/<MAUserController>
        [HttpPost("AuthenticateUser")]
        public async Task<ActionResult<MAUser>> PostAsync([FromHeader] string Email, [FromHeader] string Password)
        {
            string connString = configuration.GetConnectionString("DefaultConnection");
            MAUserVM user = new MAUserVM();
            user = await user.AuthenticateUserAsync(Email, Password, connString);

            if (user.AuthenticationCode == 0)
            {
                return Ok(user._MAUser);
            }
            else if (user.AuthenticationCode == 100)
            {
                return NotFound("Email not found");
            }
            else if (user.AuthenticationCode == 200)
            {
                return NotFound("Incorrect password");
            }
            else
            {
                return NotFound();
            }
        }

Client Code:

using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenString);
                httpClient.DefaultRequestHeaders.Add("Email", Email);
                httpClient.DefaultRequestHeaders.Add("Password", Password);
                using (var response = await httpClient.PostAsync(API_URL, stringContent))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        string apiResponse = await response.Content.ReadAsStringAsync();
                        user = JsonConvert.DeserializeObject<MAUser>(apiResponse);
                    }
                    else
                    {
                        var str = response.StatusCode;
                    }
                }
            }
                return user;

I only get Not Found in var str, but never the associated content - 'Email not found" or "Incorrect Password"

You are not reading the content of the response, only the status code.

using (var httpClient = new HttpClient()) {
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenString);
    httpClient.DefaultRequestHeaders.Add("Email", Email);
    httpClient.DefaultRequestHeaders.Add("Password", Password);
    using (var response = await httpClient.PostAsync(API_URL, stringContent)) {
        if (response.IsSuccessStatusCode) {
            string apiResponse = await response.Content.ReadAsStringAsync();
            user = JsonConvert.DeserializeObject<MAUser>(apiResponse);
        } else {
            var status = response.StatusCode;
            if (status == HttpStatusCode.NotFound 
                && response.Content.Headers.ContentLength.GetValueOrDefault() > 0) {
                string content = await response.Content.ReadAsStringAsync();
            }
        }
    }
}
return user;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM