So I have a URL on my box that when I post to it with a JSON object ("username, password, and applicationId") it will return the status code 200, 400, 505, etc... When I post to the url with Chromes Advanced Rest Client, everything works fine. So I decided to create a single page web app project that would have a login that would post to this url. Now my problem is that I can't figure out how to get the response status code. Everytime I send off the request it hits dispose so I can't get to the status code of the response.
I am losing my mind trying to figure out a way to get the response's status code. What exactly do I have to do in order to get a hold of the status code. So far I have tried using WebClient, HttpWebRequest, and HttpRequest. All of these ended up in the disposed and I couldn't get to the response. I am not really sure what to do at this point. If you can point me in the right direction it would be greatly appreciated.
public class ValidationController : ApiController
{
// POST api/account
/// <summary>
/// This will return a 200 and a JWT token if successful
/// </summary>
/// <param name="value">Json '{"Username":"[your_username]", "Password":"[your_password]","Application":"[your_application_name]"}'</param>
/// <returns>OnSuccess 200 and JWT token, OnFailure 401 and nothing else</returns>
public HttpResponseMessage Post([FromBody]Login login)
//public HttpResponseMessage Post([FromBody]string value)
{
//var login = new Login();
if (Request.RequestUri.Scheme != "https")
{
return Request.CreateResponse<string>(HttpStatusCode.HttpVersionNotSupported, "Authentication Failed. Must use HTTPS.");
}
if (login == null)
{
return Request.CreateResponse<string>(HttpStatusCode.BadRequest, "The login piece is null");
}
if (Authenticate(login) == "-1")
{
return Request.CreateResponse<string>(HttpStatusCode.Unauthorized, "Authentication Failed");
}
return CreateToken(login);
}
}
Here's how you can do it using HttpClient
:
Web API
public HttpResponseMessage Get()
{
HttpResponseMessage response;
var students = _starterKitUnitOfWork.StudentRepository.Get();
if (students == null)
{
response = new HttpResponseMessage(HttpStatusCode.NotFound);
}
else
{
response = Request.CreateResponse(HttpStatusCode.OK, students);
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
}
return response;
}
MVC Client:
public async Task<IEnumerable<Student>> GetStudents()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["BaseAddress"]);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.GetAsync("student").ConfigureAwait(false);
var statusCode = response.StatusCode; //HERE IT IS
return response.Content.ReadAsAsync<IEnumerable<Student>>().Result;
}
}
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.