2017-08-29 101 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; 
    } 
}