Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my code to return address for providing latitude and langitude of a place..
C#
public string GetLocation() 
{
    HttpWebRequest request = default(HttpWebRequest);
    HttpWebResponse response = null;
    StreamReader reader = default(StreamReader);
    string json = null;
    try 
    {
        //Create the web request   
        request = (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/geocode/json?latlng=27.7121753,85.3434027&sensor=true");
         //Get response   
            response = (HttpWebResponse)request.GetResponse();           
        //Get the response stream into a reader   
            reader = new StreamReader(response.GetResponseStream());
            json = reader.ReadToEnd();
            response.Close();
            TextBox1.Text = json;
            if (json.Contains("ZERO_RESULTS"))
            {
                TextBox2.Text = "No Address Available";
            };

            if (json.Contains("formatted_address"))
            {
                //CurrentAddress.Text = "Address Available";
                int start = json.IndexOf("formatted_address");
                int end = json.IndexOf(", Nepal");
                string AddStart = json.Substring(start + 21);
                string EndStart = json.Substring(end);
                string FinalAddress = AddStart.Replace(EndStart, ""); //Gives Full Address, One Line

                TextBox2.Text = FinalAddress;

            };
    }
    catch (Exception ex)
    {
        string Message = "Error: " + ex.ToString();
    }
}

as the code reaches response = (HttpWebResponse)request.GetResponse(); it throws an error
C#
"Unable to connect to the remote server"..

Why this is happening? can anyone help me!!!


[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v3

Hey Agustus Jackson

I have just verified it, you code works very fine. And further verified the issue you are facing is due to longer time taken by request. This happen when you have slower Internet Connection or the Request server is busy. At this very moment it works perfectly fine.

Try again buddy, All the best.
 
Share this answer
 
Comments
Codes DeCodes 6-Apr-14 7:17am    
thanks for your comment... bt i did not worked out for me.. so i did all the coding on client side.. using code for reverse geocoding.. and its working for me...
 
Share this answer
 
Hello did you get any answer to your question? I have the same problem
 
Share this answer
 
Comments
Codes DeCodes 12-Apr-14 2:21am    
you can check your firewall.. turn it off.. server side code did not worked for me... so i did coding on client side... search on google - reverse geo-coding for this.. you will get as much code as u want...
I am posting here because I have thought that below link can be helpful to solve problems faced by other/new users related to this topic.

Please put keen observation on comments/links in the mentioned link.

http://forums.asp.net/t/1334060.aspx?How+to+solve+WebException+in+Webrequest+class+Unable+to+connect+to+remote+server[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900