The remote server returned an error: 500- Internal Server error

Nov 22, 2009 at 11:29 PM

Hi All,

 

I was trying to login to google voice and make calls using c# console app. I was able to get the GALX value and also able to login to google voice using that and got the "_rnr_se" VALUE. But when I try to make the calls it says, "The remote server returned an error: 500- Internal Server error". I am also uri encoding the values like( email address, password, _rnr_se before posting to the server), but still getting the error.

Could somebody please help me with it.

Here is my code:

 

class GoogleVoiceCall
    {
        private const string PRE_LOGIN_URL = "https://www.google.com/accounts/ServiceLogin";
        private const string LOGIN_URL = "https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral";
        private const string GOOGLE_VOICE_HOME_URL = "https://www.google.com/voice";
        private const string CALL_URL = "https://www.google.com/voice/call/connect";
        private static string m_emailAddress = "*********";
        private static string m_password = "******";
        private static string m_gizmoNumber = "*******";
        private static string m_destinationNumber = "*****";  

        static void Main(string[] args)
        {
            try
            {
                // Prelogin to google voice.

                CookieContainer cookies = new CookieContainer();
                Console.WriteLine( "Logging into google.com for " + m_emailAddress + ".");
                HttpWebRequest galxRequest = (HttpWebRequest)WebRequest.Create(PRE_LOGIN_URL);
                galxRequest.ConnectionGroupName = "prelogin";
                galxRequest.CookieContainer = cookies;
                HttpWebResponse galxResponse = (HttpWebResponse)galxRequest.GetResponse();
                if (galxResponse.StatusCode != HttpStatusCode.OK)
                {
                    galxResponse.Close();
                    throw new ApplicationException("Load of the Google Voice pre-login page failed with response " + galxResponse.StatusCode + ".");
                }   
                else
                {
                    Console.WriteLine("Google Voice pre-login page loaded successfully.", m_emailAddress);
                }

                // Retreiving the GALX value

                StreamReader galxReader = new StreamReader(galxResponse.GetResponseStream());
                string galxResponseFromServer = galxReader.ReadToEnd();
                galxResponse.Close();
                Match galxMatch = Regex.Match(galxResponseFromServer, @"name=""GALX""\s+?value=""(?<galxvalue>.*?)""");
                if (galxMatch.Success)
                {
                    Console.WriteLine("GALX key " + galxMatch.Result("${galxvalue}") + " successfully retrieved.", m_emailAddress);
                }                
                else
                {
                    throw new ApplicationException("Could not find GALX key on your Google Voice pre-login page, callback cannot proceed.");
                }
                 
                Console.WriteLine("Attempting Google Voice Call");
                
                // First send a login request to get the necessary cookies.
                
                string loginData = "Email=" + Uri.EscapeDataString(m_emailAddress) + "&Passwd=" + Uri.EscapeDataString(m_password) + "&GALX=" + Uri.EscapeDataString(galxMatch.Result("${galxvalue}"));
                //string loginData = "Email=" + Uri.EscapeDataString(m_emailAddress) + "&Passwd=" + Uri.EscapeDataString(m_password);
                HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(LOGIN_URL);
                loginRequest.CookieContainer = cookies;
                loginRequest.AllowAutoRedirect = true;
                loginRequest.Method = "POST";
                loginRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
                loginRequest.ContentLength = loginData.Length;
                loginRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(loginData), 0, loginData.Length);
                HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();
                if (loginResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException("Login failed.");
                }

                else
                {
                    Console.WriteLine("Login request was successful.");
                }
                // Second send a request to the Google Voice home page to get a string key needed when placing a callback.  
                HttpWebRequest keyRequest = (HttpWebRequest)WebRequest.Create(GOOGLE_VOICE_HOME_URL);
                keyRequest.CookieContainer = cookies;
                HttpWebResponse keyResponse = (HttpWebResponse)keyRequest.GetResponse();
                if (keyResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException("_rnr_se key request failed.");
                }

                else
                {
                    Console.WriteLine("Key request was successful.");
                }
                StreamReader reader = new StreamReader(keyResponse.GetResponseStream());
                string keyResponseHTML = reader.ReadToEnd();                
                Match rnrMatch = Regex.Match(keyResponseHTML, @"name=""_rnr_se"".*?value=""(?<rnrvalue>.*?)""");
                if (!rnrMatch.Success)
                {
                    throw new ApplicationException("_rnr_se key was not found on your Google Voice home page.");
                }
                
                string rnr = rnrMatch.Result("${rnrvalue}");
                Console.WriteLine("_rnr_se key=" + rnr);
                // Thirdly (and lastly) submit the request to initiate the callback.  
                string callData = "outgoingNumber=" + Uri.EscapeDataString(m_destinationNumber) +
                "&forwardingNumber=" + Uri.EscapeDataString(m_gizmoNumber) +
                "&subscriberNumber=undefined&remember=0&_rnr_se=" + Uri.EscapeDataString(rnr);
                HttpWebRequest callRequest = (HttpWebRequest)WebRequest.Create(CALL_URL);
                callRequest.CookieContainer = cookies;
                callRequest.Method = "POST";
                callRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
                callRequest.ContentLength = callData.Length;
                callRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(callData), 0, callData.Length);
                HttpWebResponse callResponse = (HttpWebResponse)callRequest.GetResponse();
                if (callResponse.StatusCode != HttpStatusCode.OK)
                {
                    Console.WriteLine("Call request failed.");
                }

                else
                {
                    Console.WriteLine("Call request was successful.");
                }
            }
            catch (Exception excp)
            {
                Console.WriteLine("Exception Main. " + excp.Message);
            }

            finally
            {
                Console.WriteLine("finished, press any key to exit...");
                Console.ReadLine();
            }
        }
    }

Coordinator
Nov 22, 2009 at 11:42 PM

Google changed their web callback API slighlty last Tuesday (http://www.mysipswitch.com/forum/viewtopic.php?t=1941&sid=6f63c5d05dac7d8f40090b79ec8a5b4b) and you now need to add a phoneType type parameter to the call query string. If you check the latest GoogleVoiceCall.cs source you'll see the patch.

Regards.

Nov 23, 2009 at 1:40 AM

Hey.. Aaronc.. Cool... It works man.. with the string "phoneType=2" in the request string when calling .... Thanks aloot man .. Awesome..