سؤال

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);
                }


            }
    }
هل كانت مفيدة؟

المحلول

OK i found what was going on here... When the statuscode obtained in the response is "Notfound" or "Unauthorized" a Webexception is thrown so if you want to check the property of the response you need to do it inside the catch of the exception...It seems this is only the case on Windows phone... Here is the code which worked for me :

        private void GetResponseIdentificationNT(IAsyncResult MyresponseAsync)
    {

        HttpWebRequest request = (HttpWebRequest)MyresponseAsync.AsyncState;

        if (request != null)
        {
            try
            {

                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(MyresponseAsync);

                if (response.StatusCode == HttpStatusCode.OK)//l'authentification a réussi
                {
                    if (event_Authorized != null)
                        event_Authorized.Invoke();

                }   
            }

            catch (WebException we)
            {
                HttpWebResponse response = (HttpWebResponse)we.Response;

                if (response.StatusCode==HttpStatusCode.Unauthorized)
                {
                    if (event_Unauthorized != null)
                        event_Unauthorized.Invoke();

                }
                else if (response.StatusCode == HttpStatusCode.NotFound)//Server error
                {
                    if (event_NotFound != null)
                        event_NotFound.Invoke();

                }

            }

            catch (Exception ex)
            {
               throw new UnknowException(ex.Message);
            }
        }
    } 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top