Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing a get request in Java causes HTTP response code: 415

Tags:

java

http

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.

enter image description here

This is the URL I used:

https://api.datamarket.azure.com/Data.ashx/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT('jnzd27')

like image 887
Stefan Bogaard Avatar asked Jul 13 '26 17:07

Stefan Bogaard


1 Answers

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");
like image 133
ebo Avatar answered Jul 15 '26 20:07

ebo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!