您可以做的最好的办法是通过WebSocket获取像本网站那样的数据。所以首先你需要一个WebSocket客户端,在这里我使用JSR 356 - Java API for WebSocket的参考实现Tyrus。
假设你使用Maven的时候,这里有依赖关系添加到项目中的WebSocket的客户:
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>1.13</version>
</dependency>
,您将收到的JSON格式,所以你需要一个解析器的数据,你将添加到您的项目
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
这是怎样的代码看起来像:
final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
URI uri = new URI(
"wss://threatmap.checkpoint.com/ThreatPortal/websocket" +
"?X-Atmosphere-tracking-id=0" +
"&X-Atmosphere-Framework=2.2.5-javascript" +
"&X-Atmosphere-Transport=websocket" +
"&X-Atmosphere-TrackMessageSize=true" +
"&Content-Type=application/json" +
"&X-atmo-protocol=true"
);
ClientManager client = ClientManager.createClient();
try (Session session = client.connectToServer(new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
// The data is of type "number|JSON Object"
// so we remove everything before the JSON Object
message = message.substring(message.indexOf('|') + 1);
if (!message.startsWith("{")) {
// Not a JSON Object so we skip it
return;
}
// Parse the JSON Object
JSONObject jsonObject = new JSONObject(message);
if (jsonObject.has("attackname")) {
System.out.printf(
"Time: %tT Attack: %-40s Attacking Country: %-20s Target Country: %-20s%n",
Calendar.getInstance(), jsonObject.getString("attackname"),
new Locale("", jsonObject.getString("sourcecountry")).getDisplayName(),
new Locale("", jsonObject.getString("destinationcountry")).getDisplayName()
);
}
}
});
}
}, cec, uri)) {
CountDownLatch messageLatch = new CountDownLatch(1);
// Wait forever
messageLatch.await();
}
输出:
Time: 14:53:06 Attack: Trojan-Downloader.Win32.Sohanad.B Attacking Country: United States Target Country: Panama
Time: 14:53:06 Attack: Trojan-Downloader.Win32.Sohanad.B Attacking Country: United States Target Country: Panama
Time: 14:53:06 Attack: Trojan-Downloader.Win32.Sohanad.B Attacking Country: United States Target Country: Panama
Time: 14:53:06 Attack: Trojan-Downloader.Win32.Sohanad.B Attacking Country: United States Target Country: Panama
Time: 14:53:07 Attack: Trojan-Downloader.Win32.Sohanad.B Attacking Country: United States Target Country: Panama
Time: 14:53:07 Attack: Trojan-Downloader.Win32.Sohanad.B Attacking Country: United States Target Country: Panama
Time: 14:53:07 Attack: Trojan-Downloader.Win32.Sohanad.B Attacking Country: United States Target Country: Panama
Time: 14:53:07 Attack: REP.huvcru Attacking Country: France Target Country: Panama
Time: 14:53:08 Attack: REP.huvcru Attacking Country: France Target Country: Panama
我没有写在上面的代码。我想显示我想要采取的数据的位置。 – rdmzcn