1

I am trying to parse an xml response from the below url -

http://imdbapi.org/?type=xml&q=argo

For this, i have written the below code -

    try
    {
        XMLReader myReader = XMLReaderFactory.createXMLReader();
        xmlHandler handlerobj = new xmlHandler();
        myReader.setContentHandler(handlerobj);
        myReader.parse(new InputSource(new URL("http://imdbapi.org/?type=xml&q=argo").openStream()));
    }
    catch(Exception e)
    {
        System.out.println("Error");
    }       

xmlHandler is a class that extends DefaultHandler. I am getting an IOException in the above code.

Stack trace -

java.io.IOException: Server returned HTTP response code: 403 for URL:   http://imdbapi.org/?type=xml&q=argo
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at gui.getimdbdata(gui.java:73)
at gui.main(gui.java:64)

What is the problem with this code ?

6
  • 1
    Can you post the stacktrace of the Exception please? Commented Jun 6, 2013 at 7:16
  • Added the stack trace. Commented Jun 6, 2013 at 7:20
  • The server refuses to allow the requested action (response code: 403) Everything is ok, with server configuration? Are you able to get the xml file in other ways? Commented Jun 6, 2013 at 7:22
  • I have a similar android project which does the same, parses the xml response from this same URL. I can see the xml using the browser too. Commented Jun 6, 2013 at 7:24
  • 1
    This url acceps only requests from browser. You can find solution here: stackoverflow.com/questions/4797593/… Commented Jun 6, 2013 at 7:25

2 Answers 2

2

You must set the user.agent:

System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36     (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");

(if you connect to the URL with your browser this is done automagically)

Sign up to request clarification or add additional context in comments.

Comments

0

Solved the issue, thanks to @dijkstra !

The web service would only allow browser to fetch the xml data.

Following are the modifications -

       url = new URL(urlString);
       uc = url.openConnection();
       uc.addRequestProperty("User-Agent", 
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

       uc.connect();
       uc.getInputStream();
       BufferedInputStream in = new BufferedInputStream(uc.getInputStream());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.