Results 1 to 6 of 6
  1. #1
    onursahindur is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Partial GET method in Java Logical Error

    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 problem is in Scenario -2;

    The code I wrote is:

    Java Code:
    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();
        }
    }
    }
    Last edited by onursahindur; 03-13-2013 at 02:31 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Partial GET method in Java Logical Error

    Why are you expecting a 206?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    onursahindur is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Partial GET method in Java Logical Error

    To handle a partial GET request from server

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Partial GET method in Java Logical Error

    I think you misunderstood me.
    What is it about option 2 that makes you think the server should be returning a 206?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    onursahindur is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Partial GET method in Java Logical Error

    At line 74 and 78 I wrote:
    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"); // THIS CODE SHOULD SET THE PARTIAL GET METHOD

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Partial GET method in Java Logical Error

    I would look into exactly what is being sent to the server to ensure it is reconnecting.
    Use something like Fiddler2 to monitor the traffic.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Irritating logical error, switch statement problem
    By Luftwaffle in forum New To Java
    Replies: 3
    Last Post: 05-20-2012, 10:42 PM
  2. Stuck on a Do While logical error
    By dougie1809 in forum New To Java
    Replies: 8
    Last Post: 01-23-2012, 05:05 PM
  3. logical accept() method issue
    By senca in forum Networking
    Replies: 6
    Last Post: 11-01-2011, 09:34 PM
  4. Logical Operator error
    By MBD in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 12:29 PM
  5. Replies: 3
    Last Post: 09-26-2010, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •