Java - Bing空间数据服务:<title>对象移动到.... </title>

时间:2017-08-29 16:31:52

标签: bing-maps bing bing-api

我试图通过我的服务器使用Java来使用Microsoft的Bing Spatial Data Service。 (我使用了这段代码:How to send HTTP request in java?)但它根本不起作用。

我想做什么:从给定地址获取经度和经度

    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 个答案:

答案 0 :(得分:1)

看起来您使用的是Bing Maps REST服务,而不是Bing Spatial Data Services。 REST服务可以按需对各个位置进行地理编码,而Spatial Data Services可以在单个请求中对最多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;
    }
}