我发现有几个关于这个话题的讨论,但令我困惑的是另一个问题。您看,对象URL用于从特定页面获取数据。但是当页面需要权限时如何写URL?
API会建议使用卷曲代码curl -i -X GET --header 'X-Auth-code:<your_code>'
,但是如何使用?
答案 0 :(得分:2)
要在Java程序中运行命令,可以使用Process
和Runtime
。
尝试类似下面的内容:
Process p = Runtime.getRuntime().exec("curl -i -X ");
InputStream is = p.getInputStream();
答案 1 :(得分:1)
这是最终解决方案:关键是setRequestProperty()
方法
try {
String url = "http://121.41.106.89:8010/";
URL readUrl = new URL(url);
URLConnection connection = readUrl.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestProperty("X-Auth-Code", "75d07493b655591137dbc905ede428ce");
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = in.readLine();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
答案 2 :(得分:0)
URL url = new URL("http://stackoverflow.com");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
我认为this reference可能对您有帮助。