API需要GET方法,但需要发送参数

时间:2019-03-29 05:50:51

标签: java api httpurlconnection

我需要向API发送一些参数,这些参数似乎只接受GET方法...如果我加入参数,则无法通过GET方法和POST方法发送它,我得到404-找不到调用...

已经尝试了将参数连接到呼叫的各种方法,但没有成功

// Documentation - https://coinmarketcap.com/api/documentation/v1/#section/Quick-Start-Guide
String apiKey = "707e6117-e462-4de3-9748-98ab6a467f0c"; // my temp key feel free to use it 
HttpURLConnection urlConnection = null;
URL url = new URL("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("X-CMC_PRO_API_KEY", apiKey);
Map<String, String> parameters = new HashMap<>();
parameters.put("start", "1");
parameters.put("limit", "5000");
parameters.put("convert", "USD");
urlConnection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();
urlConnection.connect();
int status = urlConnection.getResponseCode();
String message = urlConnection.getResponseMessage();

我想从API获得结果

2 个答案:

答案 0 :(得分:1)

String apiKey = "707e6117-e462-4de3-9748-98ab6a467f0c"; // my temp key feel free to use it
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";

        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        map.add("start", "1");
        map.add("limit", "5000");
        map.add("convert", "USD");
        map.add("ReadTimeout", "10000");
        map.add("ConnectTimeout", "15000");
        map.add("X-CMC_PRO_API_KEY", apiKey);
        System.out.println(map);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println(response.getBody());
        }

答案 1 :(得分:1)

文档仅提及GET方法。将参数添加为标准的HTTP GET参数:

    String apiKey = "707e6117-e462-4de3-9748-98ab6a467f0c"; 
    final String request = "start=1&limit=500&convert=USD"; 
    HttpURLConnection urlConnection = null;
    URL url = new URL("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?" + request);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000);
    urlConnection.setConnectTimeout(15000);
    urlConnection.setRequestProperty("Content-Type", "application/json");
    urlConnection.setRequestProperty("X-CMC_PRO_API_KEY", apiKey);

    try (BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())))
    {
        String line = br.readLine();

        while (line != null)
        {
            System.out.println(line);
            line = br.readLine();
        }

    }