I'm trying to retrieve information from a webservice using the following code:
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);
}
}
I keep getting 415 response code when I call the URL from code while the URL works fine in chrome.

This is the URL I used:
https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT('jnzd27')
The 415 response code indicates that the media type which is requested is not supported. If you look in the response headers of chrome you can see that the returned content-type is:
Content-Type:application/atom+xml;type=entry;charset=utf-8
It seems that accepting this exact content type resolves the problem. Thus instead of:
connection.setRequestProperty("Content-Type","text/html");
use:
connection.setRequestProperty("Accept","application/atom+xml");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With