Strange thing happens in my code when i try to connect to a server... With authorized credentials the statuscode in the response's return is "OK" (im happy with that) But When i use unauthorized credentials to login the statuscode response should be "unauthorized" instead i got 'The remote server returned an error:not found". Why does the app crash ? i only changed credentials!Thanks for your help
//Request
public void ConnexionNT(string password,string user)
{
try
{
HttpWebRequest request= (HttpWebRequest)HttpWebRequest.Create(URL_CONNEXION);
request.Method = "GET";
request.Credentials = new NetworkCredential(user, password, domain);
request.CookieContainer = _cookiecontainer;
request.BeginGetResponse(new AsyncCallback(GetResponse),request);
}
catch(HttpRequestException)
{
MessageBox.Show("Un problème de connexion avec le serveur a eu lieu.", "Echec Authentification", MessageBoxButton.OK);
}
catch (Exception )
{
MessageBox.Show("Une erreur a eu lieu","Echec Authentification", MessageBoxButton.OK);
}
}
//Response
private void GetResponse(IAsyncResult MyresponseAsync)
{
HttpWebRequest request = (HttpWebRequest)MyresponseAsync.AsyncState;
if (request != null)
{
try
{
//CRASH HERE// HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(MyresponseAsync);
if(response.StatusCode==HttpStatusCode.OK)//l'authentification a réussi
{
if (event_Authorized != null)
event_Authorized.Invoke();
if (response.Cookies != null && response.Cookies.Count>0)//on récupere le cookie de navigation
{
App._cookiecollection = response.Cookies;
}
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)//l'authentification a échoué
{
if (event_Unauthorized != null)
event_Unauthorized.Invoke();
}
else if (response.StatusCode == HttpStatusCode.NotFound)//Erreur serveur
{
}
}
catch (WebException)
{
MessageBox.Show("Erreur de connexion", "erreur getresponse", MessageBoxButton.OK);
}
}
}