Skip to main content

Partial GET method in Java Logical Error

No replies
onursahindur
Offline
Joined: 2013-03-13
Points: 0

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. I am going to lose my mind a few hours later.