我尝试访问提供交通信息的开放数据网络服务。文档说请求必须GET
并且需要包含Accept: application/json
and Content-Type: application/json
。我不明白他们为什么需要Content-Type
但是好的:
我尝试仅使用Accept:
Header 检索数据,但我总是得到一个415 Unsupported Media Type
. 现在我正在尝试这种方式(但我不确定我是否真的正确设置了两个标题):
String entity = ClientBuilder.newClient().target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.get(String.class);
如您所见,我使用的是 Jersey 2.2,但我仍然得到一个415 Unsupported Media Type
.
编辑
所以我让它工作,但我不明白为什么。不是accept(MediaType.APPLICATION_JSON)
和header("Content-type","application/json")
一样吗?
String responseEntity = ClientBuilder.newClient()
.target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.header("Content-type", "application/json")
.get(String.class);