[英]Doing a get request in Java causes HTTP response code: 415
我正在尝试使用以下代码从Web服务检索信息:
String kenteken = request.getParameter("InputKenteken");
String uri = String.format("https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT('%s')", kenteken);
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type","text/html");
connection.setRequestMethod("GET");
InputStream xml = connection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
Logger.getLogger(CarServlet.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Document doc = db.parse(xml);
} catch (SAXException ex) {
Logger.getLogger(CarServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
当我从代码调用URL时,我一直得到415响应代码,而URL在chrome中工作正常。
这是我使用的URL:
https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT('jnzd27' )
415响应代码表示不支持所请求的媒体类型。 如果您查看chrome的响应标头,您可以看到返回的内容类型是:
Content-Type:application/atom+xml;type=entry;charset=utf-8
似乎接受这种确切的内容类型可以解决问题。 因此,而不是:
connection.setRequestProperty("Content-Type","text/html");
使用:
connection.setRequestProperty("Accept","application/atom+xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.