2017-08-29 9 views
0

私のサーバーからJavaを使用してMicrosoftのBing Spatial Data Serviceを使用しようとしています。 (私はこのコードを使用:How to send HTTP request in java?)しかし、それはまったく動作しません。Java - Bing空間データサービス:<title>オブジェクトが移動しました....</title>

私が何をしたい:

<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ブラウザ上でコピー&ペーストした場合、それは正常に動作します:与えられたアドレス

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; 

から緯度と経度を取得し、私は同じ結果を持っておきます...問題のどこのアイデア

答えて

1

Bing Spatial Data Servicesではなく、Bing Maps RESTサービスを使用しているようです。 RESTサービスは要求に応じて個々のロケーションをジオコードできますが、Spatial Data Servicesは1回のリクエストで最大200,000のロケーションをジオコードできます。

RESTサービスを意味するとします。はい、作成しているURLは正しいです。しかし、そうしないと、URLの一部としてURLの一部が渡されます。また、POSTリクエストではなくGETリクエストを作成する必要があります。ここで動作するはずのコードの修正版です。

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; 
    } 
} 
関連する問題