简体   繁体   English

将请求发送到Java中的Google Elevation API

[英]Send request to Google Elevation API in Java

I'm try to send Post request to Google elevation API and expecting response 我正在尝试将Post请求发送到Google海拔API并期待响应

private final String ELEVATION_API_URL =  "https://maps.googleapis.com/maps/api/elevation/json";

private final String USER_AGENT = "Mozilla/5.0";

String urlParameters = "locations=6.9366681,79.9393521&sensor=true&key=<API KEY>";


URL obj = new URL(ELEVATION_API_URL);
            java.net.HttpURLConnection con = (java.net.HttpURLConnection)obj.openConnection();

            //add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestProperty("Content-Language", "en-US");  

            String urlParameters = request;

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();

I'm sending request in this manner but I'm getting response code as 400.This is working when request sent from browser. 我正在以这种方式发送请求,但得到的响应代码为400。当从浏览器发送请求时,此代码有效。 What is wrong with this code. 此代码有什么问题。

To Allow me to get XML back I made the following changes to your project 为了让我取回XML,我对您的项目进行了以下更改

StringBuilder response = new StringBuilder(); // placed on the top of your Class

**wr.writeBytes(urlParameters.toString());** // as you have it in your code
System.out.println("ResponseMessage : " + connection.getResponseMessage());
System.out.println("RequestMethod : " + connection.getRequestMethod());

in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String inputLine;

while ((inputLine = in.readLine()) != null) {

    response.append(inputLine);

}

in.close();

wr.flush();

wr.close();

// I changed the URL to : //我将网址更改为:
private final String ELEVATION_API_URL = " https://maps.googleapis.com/maps/api/elevation/xml "; 私有的最终字符串ELEVATION_API_URL =“ https://maps.googleapis.com/maps/api/elevation/xml ”;

//**I get XML in the response** 

return response.toString();

I think there is a problem with the url parameters. 我认为url参数有问题。

Firstly because sending an empty elevation api request does return a code 400 ( Invalid request. Missing the 'path' or 'locations' parameter. ). 首先,因为发送空的海拔api请求确实会返回代码400( Invalid request. Missing the 'path' or 'locations' parameter. )。

Secondly because this works (returning 200) : 其次,因为这有效(返回200):

public void test() throws Exception {
    String ELEVATION_API_URL =  "https://maps.googleapis.com/maps/api/elevation/json";

    String USER_AGENT = "Mozilla/5.0";

    String urlParameters = "locations=6.9366681,79.9393521&sensor=true";


    URL obj = new URL(ELEVATION_API_URL + "?" + urlParameters);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setRequestProperty("Content-Language", "en-US");

    //String urlParameters = request;

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM