locked
Proxy authentication required RRS feed

  • Question

  • Hi

      I tried the following code to connect to exchange from my .NET application. It gives teh following error.

    "{"The remote server returned an error: (407) Proxy Authentication Required."}"

     System.Net.HttpWebRequest Request;
                System.Net.WebResponse Response;
                System.Net.CredentialCache MyCredentialCache;
                string strRootURI = "https://server/owa/public/Inbox";
                string strUserName = "username";
                string strPassword = "pwd";
                string strDomain = "domain";
                string strQuery = "";
                byte[] bytes = null;
                System.IO.Stream RequestStream = null;
                System.IO.Stream ResponseStream = null;
             
                try
                {
                    // Build the SQL query.
                    strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
                                + "<D:sql>SELECT \"DAV:displayname\" FROM \"" + strRootURI + "\""
                                + "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
                                + "</D:sql></D:searchrequest>";
                                 
                    MyCredentialCache = new System.Net.CredentialCache();
                    MyCredentialCache.Add(new System.Uri(strRootURI),"NTLM",new System.Net.NetworkCredential(strUserName, strPassword, strDomain));
                                 
                    Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
                                
                    Request.Credentials = MyCredentialCache;
                    Request.Method = "SEARCH";
                    bytes = Encoding.UTF8.GetBytes((string)strQuery);
                    Request.ContentLength = bytes.Length;
                                
                    RequestStream = Request.GetRequestStream();                //Proxy authentication required error in tis line
                   
                   
                    RequestStream.Write(bytes, 0, bytes.Length);
                    RequestStream.Close();
                    Request.ContentType = "text/xml";
                             
                    Response = (HttpWebResponse)Request.GetResponse();
                              
         
                    Response.Close();

                }
                catch (Exception ex)
                {
                    
                }

    How to give proxy authentication credentials  and connect to exchagne server and read emails?

    Thanks
    Ashok

    Monday, June 29, 2009 3:19 PM

Answers


  • Dear Ashok !

            I use Exchange Webservice that too in different fashion.

            Here I have modified your code by considering general practice to authenticate request. That too I use System.Net.WebRequest class but yours is HttpWebRequest.

            anyhow the authentication method should remain same. So, I have modified as per my knowledge and convenience. Check the following solution whether it is working.


                System.Net.HttpWebRequest cRequest;
                System.Net.WebResponse cResponse;
                System.Net.CredentialCache MyCredentialCache;
                string strRootURI = "https://server/owa/public/Inbox";
                string strUserName = "username";
                string strPassword = "pwd";
                string strDomain = "domain";
                string strQuery = "";
                byte[] bytes = null;
                System.IO.Stream RequestStream = null;
                System.IO.Stream ResponseStream = null;
              
                try
                {
    
    
                strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
                + "<D:sql>SELECT \"DAV:displayname\" FROM \"" + strRootURI + "\""
                + "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
                + "</D:sql></D:searchrequest>";
    
                //MyCredentialCache = new System.Net.CredentialCache();
                //MyCredentialCache.Add(new System.Uri(strRootURI), "NTLM", new System.Net.NetworkCredential(strUserName, strPassword, strDomain));
    
                cRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
    
                //Request.Credentials = MyCredentialCache;
                cRequest.Method = "SEARCH";
                bytes = Encoding.UTF8.GetBytes((string)strQuery);
                cRequest.ContentLength = bytes.Length;
    
    
    
                System.Net.NetworkCredential cReqCredentials = new System.Net.NetworkCredential(strUserName, strPassword);
                System.Net.WebProxy cReqProxy = new System.Net.WebProxy("http://192.168.0.x:8080");
                cReqProxy.Credentials = cReqCredentials;
                cRequest.Proxy = cReqProxy;
    
    
    
                RequestStream = cRequest.GetRequestStream();                //Proxy authentication required error in tis line
    
    
                RequestStream.Write(bytes, 0, bytes.Length);
                RequestStream.Close();
                
                    //Request.ContentType = "text/xml"; // I did't understand why this line is required.
    
                cResponse = (HttpWebResponse)cRequest.GetResponse();
    



         Moreover I don't know whether that writing to stream will work all the times, because I know a little diffrent concept with loop. Why because, the total bytes can't be written once. You have to check how much is successfully written and then try for the remaining content even though you mentioned content length.

        try the above solution and if the error changed then reply.

    Regards





    I am a small programmer
    • Marked as answer by Ashok K Tuesday, July 14, 2009 4:26 PM
    Friday, July 10, 2009 10:02 AM

All replies


  • Dear Ashok !

            I use Exchange Webservice that too in different fashion.

            Here I have modified your code by considering general practice to authenticate request. That too I use System.Net.WebRequest class but yours is HttpWebRequest.

            anyhow the authentication method should remain same. So, I have modified as per my knowledge and convenience. Check the following solution whether it is working.


                System.Net.HttpWebRequest cRequest;
                System.Net.WebResponse cResponse;
                System.Net.CredentialCache MyCredentialCache;
                string strRootURI = "https://server/owa/public/Inbox";
                string strUserName = "username";
                string strPassword = "pwd";
                string strDomain = "domain";
                string strQuery = "";
                byte[] bytes = null;
                System.IO.Stream RequestStream = null;
                System.IO.Stream ResponseStream = null;
              
                try
                {
    
    
                strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
                + "<D:sql>SELECT \"DAV:displayname\" FROM \"" + strRootURI + "\""
                + "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
                + "</D:sql></D:searchrequest>";
    
                //MyCredentialCache = new System.Net.CredentialCache();
                //MyCredentialCache.Add(new System.Uri(strRootURI), "NTLM", new System.Net.NetworkCredential(strUserName, strPassword, strDomain));
    
                cRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
    
                //Request.Credentials = MyCredentialCache;
                cRequest.Method = "SEARCH";
                bytes = Encoding.UTF8.GetBytes((string)strQuery);
                cRequest.ContentLength = bytes.Length;
    
    
    
                System.Net.NetworkCredential cReqCredentials = new System.Net.NetworkCredential(strUserName, strPassword);
                System.Net.WebProxy cReqProxy = new System.Net.WebProxy("http://192.168.0.x:8080");
                cReqProxy.Credentials = cReqCredentials;
                cRequest.Proxy = cReqProxy;
    
    
    
                RequestStream = cRequest.GetRequestStream();                //Proxy authentication required error in tis line
    
    
                RequestStream.Write(bytes, 0, bytes.Length);
                RequestStream.Close();
                
                    //Request.ContentType = "text/xml"; // I did't understand why this line is required.
    
                cResponse = (HttpWebResponse)cRequest.GetResponse();
    



         Moreover I don't know whether that writing to stream will work all the times, because I know a little diffrent concept with loop. Why because, the total bytes can't be written once. You have to check how much is successfully written and then try for the remaining content even though you mentioned content length.

        try the above solution and if the error changed then reply.

    Regards





    I am a small programmer
    • Marked as answer by Ashok K Tuesday, July 14, 2009 4:26 PM
    Friday, July 10, 2009 10:02 AM
  • If you feel above is the correct answer or it helped you anyway, please click "Propose as Answer"
    I am a small programmer
    Monday, July 13, 2009 5:47 AM