0

I'm trying to use Bing Spatial Data Service of Microsoft by using Java from my server. (I used this code : How to send HTTP request in java?) but it doesnt work at all.

What I want to do: get latitude and longitude from a given adress

    public static void main(String[] args) throws IOException {


    System.out.println(SendRequete());

}

static String SendRequete(){


    String bingMapsKey = "zzzzzzzzzz";
    String contentType="text/plain";
    String targetURL = "http://dev.virtualearth.net/";
    String urlParameters="REST/v1/Locations?countryRegion=France&locality=Paris&postalCode=75001&addressLine=rue%20de%20la%20paix&key=" + bingMapsKey;
    System.out.println(targetURL+urlParameters);

    try{
    HttpURLConnection connection = null;

     URL url = new URL(targetURL);
        connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", contentType);

    connection.setRequestProperty("Content-Length", 
            Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");  

    connection.setUseCaches(false);
    connection.setDoOutput(true);

    //request
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.close();


    //Get Response  
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    StringBuffer response = new StringBuffer(); // or StringBuffer if Java version 5+
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
      response.append(line);
      response.append('\r');
    }
    rd.close();
    return response.toString();
  } catch (Exception e) {
    e.printStackTrace();
    return null;

I keep having the same results:

 <html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://msdn.microsoft.com/en-us/library/dd877180.aspx">here</a>.</h2>
</body></html>
</body></html>ed to <a href="https://msdn.microsoft.com/en-us/library/dd877180.aspx">here</a>.</h2>
ml><head><title>Object moved</title></head><body>

If I copy and paste on y browser it works fine... Any idea of where the problem is

Jean
  • 521
  • 6
  • 17

1 Answers1

1

Looks like you are using the Bing Maps REST services not the Bing Spatial Data Services. The REST services can geocode individual locations on demand, while the Spatial Data Services can geocode up to 200,000 locations in a single request.

Assuming you mean the REST services, yes, the URL you are creating is correct. However you are passing in part of the URL as URL parameters when you shouldn't be. Also, you need to make a GET request, not a POST request. Here is a modified version of your code that should work.

static String SendRequete(){

    String bingMapsKey = "zzzzzzzzzz";
    String contentType="text/plain";
    String targetURL = "http://dev.virtualearth.net/";
    String urlParameters="REST/v1/Locations?countryRegion=France&locality=Paris&postalCode=75001&addressLine=rue%20de%20la%20paix&key=" + bingMapsKey;
    System.out.println(targetURL+urlParameters);

    try{
        URL url = new URL(targetURL + urlParameters);
        URLConnection connection = url.openConnection();

        //Get Response  
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        StringBuffer response = new StringBuffer(); // or StringBuffer if Java version 5+
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
rbrundritt
  • 14,135
  • 2
  • 19
  • 41