Retrieve geographic data via an HTTP request via Google Maps URL

3

Good morning guys.

I'm trying to retrieve some geographic information based on latitude and longitude.

To retrieve such information I'm trying to make a request via URL through this link: Google Maps Geolocation API . It returns me an XML where I can work on it.

When creating this service in my web service, I uploaded it to the developing Weblogic server (running in LINUX system) and it returns the following error when executing it:

java.io.FileNotFoundException: Response: '502: Bad Gateway' for url: 'http://maps.google.com/maps/api/geocode/xml?latlng=-15.8040195453225%2C-47.8799401993617'

I have tried it in many ways, but it still gives the same error. I am using Google's own code to perform the consumption of information, as follows:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public class GeocodeProcessor {

    private static final String GEOCODE_REQUEST_PREFIX = "http://maps.google.com/maps/api/geocode/xml";

    private static final int PROXY_PORT                 = 3128;
    private static final String PROXY_HOST              = "vampre.funasa.gov";
    private static final int TIMEOUT                    = 30000;


    public static void main() throws IOException,
            URISyntaxException, ParserConfigurationException, SAXException {

        String urlString = null;

        urlString = GEOCODE_REQUEST_PREFIX + "?latlng=" + URLEncoder.encode("-15.8040195453225,-47.8799401993617", "UTF-8");
        System.out.println(urlString);

        // Convert the string to a URL so we can parse it
        URL url = new URL(urlString);

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                PROXY_HOST, PROXY_PORT));

        HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
        conn.setConnectTimeout(TIMEOUT);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/xml");

        try {
            // open the connection and get results as InputSource.
            conn.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),
                    "UTF-8"));

            String line = null;
            StringBuilder ab = new StringBuilder();
            while( ( line = reader.readLine() ) != null )  {
                ab.append(line);
            }
            reader.close();

            System.out.println(ab.toString());

        } finally {
            conn.disconnect();
        }
    }

}

An aid would be extremely useful now. Vlw galerinha.

    
asked by anonymous 14.10.2015 / 14:13

1 answer

2

@Bruno is correct! Giving a bypass on your proxy , the code ran normally!

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

I was wondering, however, if there was anything wrong with the proxy code - but it seems that not - I installed a proxy server to test - log of the program:

Outputfromyourprogram:

Theproxyprogramusedwas WinGate .

    
21.10.2015 / 06:28