1

I implemented Java Network program that reads a txt file from an HTML content. I was able to use HTML_OK scenario but When I am trying to get a "Partial GET" request, the connection returns again "HTML_OK". I could not find out why does this happen, I searched the internet but I could not find any answer. The code I wrote is:

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class FileDownloader {


    public static void main(String[] args){

        try{

            int bufSize = 8 * 1024;
            URL url = null;
            BufferedInputStream fromURL = null;
            BufferedOutputStream toFile = null;

            /*
            if(args[1].charAt(0) == 'h' && args[1].charAt(1) == 't' &&
                    args[1].charAt(2) == 't' && args[1].charAt(2) == 'p'){
                url = new URL(args[1]); 
            }
            else{
                url = new URL("http://" + args[1]);
            }
            * 
            */
            // silinecek
            url = new URL("http://www.cs.bilkent.edu.tr/~morhan/cs421/file_2.txt"); 

            // Conncecting the URL to HttpURLConnection
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // Setting up the outputfileName
            String outputfileName = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
            File outputFile = new File(outputfileName);

            // Scenario - 1 ( 200 OK Message From HTML )
            if(args.length == 3){ // 3 OLACAK
                con.setRequestMethod("GET");
                System.out.println("Size of the file is: " + con.getContentLength());
                fromURL = new BufferedInputStream(con.getInputStream(), bufSize);
                toFile = new BufferedOutputStream(new FileOutputStream(outputFile), bufSize);   

                if(con.getResponseCode() == HttpURLConnection.HTTP_OK){
                    // READING BYTE BY BYTE HERE
                    int read = -1;
                    byte[] buf = new byte[bufSize];
                    while ((read = fromURL.read(buf, 0, bufSize)) >= 0) {
                        toFile.write(buf, 0, read);
                    }
                    toFile.close();
                    System.out.println("ok");
                }
            // Scenario - 2 (206 Partial Get Message From HTML
            }else if(args.length == 0){ // 5 OLACAK
                con.setRequestMethod("HEAD");

                if(con.getResponseCode() == HttpURLConnection.HTTP_OK){

                    System.out.println("Size of the file is: " + con.getContentLength());

                    //byte startRange =  0; //Byte.parseByte(args[3]);
                    //byte finishRange =  24;//Byte.parseByte(args[4]);

                    if(startRange < 0 || finishRange > ((byte)con.getContentLength()) - 1
                            || startRange > finishRange){
                        System.out.println("Range is not OK.");
                    }else{                     


                        con.setRequestMethod("GET");

                        // I am Setting the range here, however the program 
                        // always returns 200 OK message instead of a 206 one
                        con.setRequestProperty("Range: ", "bytes=0-20"); 

                        System.out.println(con.getRequestMethod());

                        fromURL = new BufferedInputStream(con.getInputStream(), bufSize);
                        toFile = new BufferedOutputStream(new FileOutputStream(outputFile), bufSize);

                        System.out.println(con.getResponseCode());

                        if(con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){
                            // NOT DOING THE IF STATEMENT
                            System.out.println("aaaa");
                        }

                        System.out.println("bbbb");
                    }
                }
            }else{
                System.out.println("Wrong argument count.");
            }

        }catch(MalformedURLException mue){
            mue.printStackTrace();
        }catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

Can anyone help me abut this?

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
Onur Şahindur
  • 492
  • 8
  • 18
  • Why would you anything other than a `HTTP_OK` when using the `HEAD` method? The http spec clearly states - *the HEAD method is identical to GET except that the server MUST NOT return a message-body in the response* – Nick Holt Mar 13 '13 at 12:49
  • Both of your request says "GET". How are you trying Partial GET – Joe2013 Mar 13 '13 at 12:50
  • @NickHolt I could not understand what you mean. Can you be a little bit clear? – Onur Şahindur Mar 13 '13 at 12:51
  • @Joe2013 I am setting the Partial get method by writing; con.setRequestMethod("GET"); con.setRequestProperty("Range: ", "bytes=0-20"); – Onur Şahindur Mar 13 '13 at 12:51
  • @Onur sorry, reading the rest of your code I see where you're expecting the 206. I think you're specifying the range incorrectly. Instead of `con.setRequestProperty("Range: ", "bytes=0-20")` try `con.setRequestProperty("Range", "bytes=0-20")` as specified here - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35 – Nick Holt Mar 13 '13 at 13:02
  • @NickHolt I also tried that one but I am getting the 200 OK message instead of 206 Partial one :(. – Onur Şahindur Mar 13 '13 at 13:09
  • @Onur are you sure the server supports partial gets? You might want to try with Apache's HttpClient, if it doesn't work there then the problem is on the server. – Nick Holt Mar 13 '13 at 13:36
  • @NickHolt how can I try Apache's HttpClient, am I going to change the URL? – Onur Şahindur Mar 13 '13 at 18:06

0 Answers0