0

Small snippet of program challenging me from a day,

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import com.rometools.rome.io.FeedException;

public class Test {

public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream openStream = new URL("http://www.livemint.com/rss/money").openStream();
        Document doc = db.parse(openStream);
        System.out.println(doc.getDocumentURI());
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

All this experiment started from the rome tools parser code, which was giving me same error in different approaches also.

SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(url));
feed.getEntries();

Exception was getting thrown in creating new xml Reader, so i wrote a test program in different approach. Now both are throwing the same error,

[Fatal Error] :1:1: Premature end of file.

I have to fetch xml response from a url, but not able to do.

Sunil Rk
  • 999
  • 6
  • 12
  • 35

2 Answers2

1

I believe this is because the server is trying to redirect you to use https instead of http, while HttpURLConnection does not support that automatically.

Changing your URL to use https would solve the issue.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • might be not sure, but this code was working ,SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(new XmlReader(url)); feed.getEntries(); Suddenly started breaking like this. – Sunil Rk Oct 12 '18 at 06:48
0

Sending a GET request to this URL from Java Code says:

Sending 'GET' request to URL : http://www.livemint.com/rss/money
Response Code : 301

So the XML is not present in the response as the request fails. Please check the API and see if you have to include any extra parameters in the HTTP request.

I used following code to connect to the URL:

  public static String getUrlResponse(String url) throws IOException {
             URL obj = new URL(url);
             HttpURLConnection con = (HttpURLConnection) obj.openConnection();
             // optional default is GET
             con.setRequestMethod("GET");
             //add request header
             int responseCode = con.getResponseCode();
             System.out.println("\nSending 'GET' request to URL : " + url);
             System.out.println("Response Code : " + responseCode);
             BufferedReader in = new BufferedReader(
                     new InputStreamReader(con.getInputStream()));
             String inputLine;
             StringBuffer response = new StringBuffer();
             while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
             }
             in.close();
             //print in String
             return response.toString();
  }
S.K.
  • 3,597
  • 2
  • 16
  • 31