locked
Asynchronous Server to Server Call to Google Place API does not return response/Fails RRS feed

  • Question

  • User1033586979 posted

    I want to fetch City Names from Google.for that I have actiavated Place API using developer Account.

    i have written Asynchronous routine that uses object of HttpWebRequest class to request Google Api and object of the IAsyncResult Interface to receive the resposne

    below is my code snippet

        [WebMethod]
        public string[] GetGoogleCityName(string prefixText)
        {
    
    
            try
            {
                resp = new object();
                string value = "";
                string Prevar = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=";
                string PostVar = "&sensor=false&types=(regions)&key=" + System.Configuration.ConfigurationManager.AppSettings.Get("GoogleAPIKey");
                Prevar = Prevar + prefixText + PostVar;
                HttpWebRequest webRequest = WebRequest.Create(Prevar) as HttpWebRequest;
                webRequest.Timeout = 20000000;
                webRequest.Method = "GET";
                webRequest.KeepAlive = false;
                webRequest.ProtocolVersion = HttpVersion.Version10;
                IAsyncResult result = webRequest.BeginGetResponse(new AsyncCallback(RequestCompleted), webRequest);
                Thread.Sleep(500);
    
            }
            catch (Exception ex)
            {
                return null;
            }
    
            string[] names = this.ConvertToBrowserJSONString(_cities).ToArray();
            if (names.Length > 0)
            {
                return names.ToArray();
            }
            return null;
            
        }
          
      private void RequestCompleted(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState;
            
            var response = (HttpWebResponse)request.EndGetResponse(result);
    
            using (var stream = response.GetResponseStream())
            {
                var r = new StreamReader(stream);
                resp = r.ReadToEnd();
                // List<City> myDeserializedObjList = (List<City>)Newtonsoft.Json.JsonConvert.DeserializeObject(resp, typeof(List<City>));
               
                _cities = JsonConvert.DeserializeObject<GoogleCity>(resp.ToString());
                
            }
        }

    but on var response = (HttpWebResponse)request.EndGetResponse(result); it throws the exception "Underlying Connection was closed:UnExpted Error On recieve"

    I have tried Setting Keep-Alive Property of HttpWebRequest class both to true or false.but none of them works.

    Please Help me on this issue.



    Monday, October 21, 2013 4:49 AM

All replies

  • User-211766943 posted

    Does your code works without async call. i.e. Instead of 

    BeginGetResponse call GetResponse does it works.
    Monday, October 21, 2013 7:54 AM
  • User1033586979 posted

    Same issue persist in Sync call

    Monday, October 21, 2013 8:38 AM
  • User-211766943 posted

    I run your code and I'm getting response on WebResponse response = webRequest.GetResponse();

    Monday, October 21, 2013 8:52 AM
  • User1033586979 posted

    Thanks for your effort shridhar.Issue is not of synchronicity/Asynchronicity

    This code runs in fresh application but somehow not in my project.

    to be more specific.Inner Exception of this issue is

    "{"Unable to find an entry point named 'EnumerateSecurityPackagesW' in DLL 'security.dll'.":""}"

    i have searched many forums but theres no certain solution for this.

    Monday, October 21, 2013 9:08 AM