Keith
Keith

Reputation: 635

Java URL - Server Response 416 error code

I'm trying to get the html source of a specific website I once built. However I continue to get this error:

java.io.IOException: Server returned HTTP response code: 416 for URL: http://www.website.com/
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
    at main.getHTMLContent(main.java:37)
    at main.getEventLinks(main.java:69)
    at main.main(main.java:21)
Exception in thread "main" java.lang.NullPointerException
    at main.getEventLinks(main.java:72)
    at main.main(main.java:21)

I have done a bit of research and I could not find a possible solution.

I know the 416 is :

The Web server (running the Web site) thinks that the HTTP data stream sent by the client (e.g. your Web browser or our CheckUpDown robot) contains a 'Range' request which specifies a range of bytes which can not be satisfied - because the resource being accessed does not cover this byte range. For example if the resource - an image file for example - has 1000 bytes and the Range requested is 500-1500, then it can not be satisfied.

via http://www.checkupdown.com/status/E416.html

I found this solution but I dont have access to the server.

416 Requested Range Not Satisfiable

If you know of a way to fix this, it would be helpful. I am also open to possible alternatives.

Here is the code making the request:

protected  static ArrayList<String> getHTMLContent(String u){
    URL url;
    ArrayList<String> linesOfHTML = new ArrayList<>();
    try {
        // get URL content

        //String a="http://localhost:8080//TestWeb/index.jsp";
        url = new URL(u);
        URLConnection conn = url.openConnection();

        // open the stream and put it into BufferedReader
        BufferedReader br = new BufferedReader(
                           new InputStreamReader(conn.getInputStream()));

        String inputLine;
        while ((inputLine = br.readLine()) != null) {
                System.out.println(inputLine);
                if(!inputLine.equals("")){
                    linesOfHTML.add(inputLine);
                }
        }
        br.close();

        System.out.println("Done");
        return null;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Upvotes: 3

Views: 2044

Answers (1)

Sargis Aleksanyan
Sargis Aleksanyan

Reputation: 41

I know I am very late but my answer might be helpful.By setting request headers I solved the problem

conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 

or it might return simply return response in json format you have to write

conn.setRequestProperty("Accept" ,"application/json"); 

But please note this code will not work if server blocks you intentionally.

Upvotes: 2

Related Questions