2017-08-29 100 views
0

我試圖通過從我的服務器使用Java來使用Microsoft的Bing Spatial Data Service。 (我用這個代碼:How to send HTTP request in java?),但它根本不起作用。Java - Bing空間數據服務:<title>對象已移至...</title>

我想要做的:獲得緯度和經度從給定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; 

我一直有同樣的結果:

<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> 

如果我複製並粘貼Y上的瀏覽器,它工作正常...任何想法問題出在哪裏

回答

1

看起來您正在使用Bing地圖REST服務而不是Bing Spatial Data Services。 REST服務可以根據需要對各個位置進行地理編碼,而空間數據服務可以在單個請求中對多達200,000個位置進行地理編碼。

假設你指的是REST服務,是的,你創建的URL是正確的。但是,如果不應該,則會將URL的一部分作爲URL參數傳遞。此外,您需要進行GET請求,而不是POST請求。這是你應該工作的代碼的修改版本。

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; 
    } 
}