Erreur 403 puis erreur 500 - HttpWebResponse

FlyStd -  
 W95PSP -
Bonjour,

J'essaye de me connecter sur un serveur exchange via https...
J'ai essayé plusieurs méthodes dont celle ci qui provient d'ici:
https://blog.mbcharbonneau.com/2006/10/26/using-net-and-webdav-to-access-an-exchange-server/

Mais il me réenvoyait une erreur 403 alors qu'il y a le bon mot de passe et login...
Après avoir ajouté cette ligne là: request.UseDefaultCredentials = true;
il me réenvoie erreur 500 (interne au serveur)

Le programme plante ici:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

il est en attente d'une réponse, on dirait...

je ne comprends pas... meme après de multiples recherches...
Voici mon code:
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Text.RegularExpressions;
using System.Security;

namespace DowntownSoftwareHouse.ExchangeExample
{
    public class WebDAVRequest
    {
        private static string server = "https://ip";
        private static string path = "/exchange/boite/Inbox";
        private static string username = "domaine\\user";
        private static string password = "password";
        private CookieContainer authCookies = null;


        /// 
        /// Add code here to check the server certificate, if you think it’s necessary. Note that this
        /// will only be called once when the application is first started.
        /// 

        protected bool CheckCert(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

        /// 
        /// Default WebDAVRequest constructor. Set the certificate callback here.
        /// 

        public WebDAVRequest()
        {
            ServicePointManager.ServerCertificateValidationCallback += CheckCert;
        }


        /// 
        /// Authenticate against the Exchange server and store the authorization cookie so we can use
        /// it for future WebDAV requests.
        /// 

        public void Authenticate()
        {
         //**************************************************************************************************
                string authURI = server + "/exchweb/bin/auth/owaauth.dll";
                Console.WriteLine("uri: " + authURI);

         // Create the web request body:

         string body = string.Format( "destination={0}&username={1}&password={2}", server + path, username, password );
         byte[] bytes = Encoding.UTF8.GetBytes( body );

         // Create the web request:

         HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create( authURI );
         request.Method = "POST";
         request.ContentType = "application/x-www-form-urlencoded";
         request.CookieContainer = new CookieContainer();
         request.ContentLength = bytes.Length;
         request.UseDefaultCredentials = true;

         // Create the web request content stream:

         using ( Stream stream = request.GetRequestStream() )
         {
             stream.Write( bytes, 0, bytes.Length );
             stream.Close();
         }

         // Get the response & store the authentication cookies:
         Console.WriteLine("en attente ... ");
         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
         Console.WriteLine(" ... attente finie");

         if ( response.Cookies.Count < 2 )
             throw new AuthenticationException( "Login failed. Is the login / password correct?" );

         CookieContainer cookies = new CookieContainer();
         foreach ( Cookie myCookie in response.Cookies )
         {
             cookies.Add( myCookie );
         }

         response.Close();
           
        }







        static void Main(string[] args)
        {
            try
            {
                WebDAVRequest request = new WebDAVRequest();
                Console.WriteLine("apres init");
                request.Authenticate();
                Console.WriteLine("apres auth");
               
            }
            catch (SecurityException e)
            {
                Console.WriteLine("Security Exception");
                Console.WriteLine("   Msg: " + e.Message);
                Console.WriteLine("   Note: The application may not be trusted if run from a network share.");
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine("Authentication Exception, are you using a valid login?");
                Console.WriteLine("   Msg: " + e.Message);
                Console.WriteLine("   Note: You must use a valid login / password for authentication.");
            }
            catch (WebException e)
            {
                Console.WriteLine("Web Exception");
                Console.WriteLine("   Status: " + e.Status);
                Console.WriteLine("   Reponse: " + e.Response);
                Console.WriteLine("   Msg: " + e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown Exception");
                Console.WriteLine("   Msg: " + e.Message);
            }
        }
    }
}


Quelqu'un aurait-il une piste?
une réponse à cette erreur lancée?

Merci d'avance

1 réponse

W95PSP
 
Je vient de trouver, il faut renseigner l'user agent :
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
(Par exempler)

Ha, je suis content, ça faisait pas mal de temps que je le cherchait !
J'espère avoir aidé quelqu'un alors :)
2