java - HTTP 从 API 获取 XML 请求

标签 java xml api get http-headers

我对在 java 中调用 API 来请求 XML 非常陌生,因此我使用了模板。如下所示。

但是,现在我从另一个 Web API 调用 API,他们要求我使用我一无所知的 python,或者使用 3rd party HTTP web client 。相反,我希望 API 的调用发生在我的 java 应用程序本身中。是否可以使用以下 header 更改以下代码?

Sample request URL: http:// datamall.mytransport.sg/ltaodataservice.svc/TravelTimeSet

Request headers: AccountKey=xxx, UniqueUserId=xxx, accept=application/atom+xml

API documentation/guide

NowCast.java *NowCast.java 链接到 SAX 解析器

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NowCast extends SAXParser {

public static void main(String[] args) {

    String datasetName, keyRef;
    datasetName = "Nowcast";
    keyRef = "781CF461BB6606ADEA6B1B4F3228DE9DB26ABF1B1B755E93";

    NowCast od = new NowCast();

    try {
        od.callWebAPI(datasetName, keyRef);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void callWebAPI(String datasetName, String keyRef) throws Exception {
    // Step 1: Construct URL
    String url = "http://www.nea.gov.sg/api/WebAPI?dataset=" + datasetName
            + "&keyref=" + keyRef;

    // Step 2: Call API Url
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);
    // Step 3: Check the response status
    if (responseCode == 200) {
        // Step 3a: If response status == 200
        // print the received xml
        super.Parserr(con.getInputStream());
        System.out.println(readStream(con.getInputStream()));
    } else {
        // Step 3b: If response status != 200
        // print the error received from server
        System.out.println("Error in accessing API - "
                + readStream(con.getErrorStream()));
    }
}

// Read the responded result
private String readStream(InputStream inputStream) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            inputStream));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = reader.readLine()) != null) {
        response.append(inputLine);
    }
    reader.close();
    return response.toString();
}

}

最佳答案

我将Python代码(在API文档中)与上面的代码交叉引用并解决了问题

在java中设置请求 header

URL obj = new URL(url);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();

con.setRequestMethod("GET");

con.setRequestProperty("AccountKey", "zIIkxPOqBRxnkH97G6ZGlA==");

con.setRequestProperty("UniqueUserID", "5ccef4a0-10e2-4f75-8f4d-2be67885b10f");

con.setRequestProperty("accept", "application/atom+xml");

关于java - HTTP 从 API 获取 XML 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078963/

相关文章:

java - 在 java 中创建样式 xml 以供查看(按钮)

java - 是什么阻止我将 Map<String, List<String>> 作为 Map<String, Collection<String>> 返回?

Java 异步 MySQL 查询

java - 如何从现有的基于 XML 的 RESTful 服务创建 Java Jersey 客户端

xml - 在 actionscript 中,检查 xml 节点属性是否存在的最佳方法是什么?

java - 如何用SVNkit识别SVN路径是目录还是文件?

java - 希望自定义键盘仅用于我的应用程序并在应用程序失去焦点时恢复以前的键盘

api - Redis NOAUTH 需要身份验证。 [TCP ://127. 0.0.1:6379] Laravel

api - 用于从文章 url 中提取文本的 Web API?

api - 如何添加更多数据以存储在 jenkins rest api 中