我给你把程序死的问题解决了,另外的逻辑我把思路写在程序中了import java.net.*; import java.io.*; public class SimpleWebClient { public static void main(String[] args){ Socket sock = null; InputStream isIn = null; PrintStream psOut = null; byte abDataIn[] = new byte[1024]; int iNumBytes; try { //I make the web server to my localhost,and port 8000 sock = new Socket("127.0.0.1", 8000); isIn = sock.getInputStream(); psOut = new PrintStream(sock.getOutputStream());
System.out.println("Clnt: Sending a GET request..."); psOut.println("GET /index.html HTTP/1.1"); psOut.println("Connection: Keep-Alive"); psOut.println("User-Agent: Mozilla/2.0GoldB1 (Win95; I)"); psOut.println("Pragma: no-cache"); psOut.println("Host: localhost"); psOut.println("Accept: image/gif, image/x-xbitmap, " + "image/jpeg, image/pjpeg, */*"); psOut.println (""); System.out.println("Clnt: Sent a GET request...\n");
/* while(true){ //the program will wait the inputstream to receive the other 1024 bytes //if can not get the bytes, the program will freeze util get the bytes iNumBytes = isIn.read(abDataIn, 0, 1024); if (iNumBytes < 0) {
break; } System.out.println("-------------Received--------------- " + iNumBytes + " bytes"); System.out.println(new String (abDataIn, 0, 0, iNumBytes)); System.out.println("------------------------------------\n"); }*/ while(isIn.available() > 0){ iNumBytes = isIn.read(abDataIn, 0, 1024); /* come here you can get the last-modified information use this infomation to compare to your given time point, if the last-modified field is later than the time your teacher given then return the full response else return code 304 It is too cumbersome to write that code. And because your use socket and inputstream instead of Http... class, so I can not use the method Http... Class defined I think u can use readLine() method, so u can deal with each Line one by one if u find the line with "last-modified", then get the time info just so so, good luck to u if u can not understand what i said, email to [email protected] Good Night
当然还有If-Unmodified-Since,If-Match 和If-None-Match(用于比较Etag里的信息),这些条件都是http中的东西,被要求要和程序中的get request配合使用。我是实在不知道怎么在java socket的程序中使用这些http的条件。
好了,整个我的project就是这样了,不知道是不是够详细。 要是谁有办法一定要帮帮我,高手们一定应该不会觉得这东西难吧,可是我在澳洲这鬼地方找资料都没有办法,英文又差,要是下个星期再交不了程序就死定了。(这个程序可就是1门课呀,2000澳币呀!)
我的email是[email protected] 或者[email protected](英文)
import java.io.*; public class SimpleWebClient {
public static void main(String[] args){
Socket sock = null;
InputStream isIn = null;
PrintStream psOut = null;
byte abDataIn[] = new byte[1024];
int iNumBytes; try {
//I make the web server to my localhost,and port 8000
sock = new Socket("127.0.0.1", 8000);
isIn = sock.getInputStream();
psOut = new PrintStream(sock.getOutputStream());
System.out.println("Clnt: Sending a GET request...");
psOut.println("GET /index.html HTTP/1.1");
psOut.println("Connection: Keep-Alive");
psOut.println("User-Agent: Mozilla/2.0GoldB1 (Win95; I)");
psOut.println("Pragma: no-cache");
psOut.println("Host: localhost");
psOut.println("Accept: image/gif, image/x-xbitmap, "
+ "image/jpeg, image/pjpeg, */*");
psOut.println ("");
System.out.println("Clnt: Sent a GET request...\n");
/*
while(true){
//the program will wait the inputstream to receive the other 1024 bytes
//if can not get the bytes, the program will freeze util get the bytes
iNumBytes = isIn.read(abDataIn, 0, 1024);
if (iNumBytes < 0) {
break;
}
System.out.println("-------------Received--------------- "
+ iNumBytes + " bytes");
System.out.println(new String (abDataIn, 0, 0, iNumBytes));
System.out.println("------------------------------------\n");
}*/
while(isIn.available() > 0){
iNumBytes = isIn.read(abDataIn, 0, 1024);
/*
come here you can get the last-modified information
use this infomation to compare to your given time point,
if the last-modified field is later than the time your teacher given
then return the full response
else return code 304
It is too cumbersome to write that code. And because your use socket and inputstream
instead of Http... class, so I can not use the method Http... Class defined
I think u can use readLine() method, so u can deal with each Line one by one
if u find the line with "last-modified", then get the time info
just so so, good luck to u
if u can not understand what i said, email to [email protected]
Good Night
ps: the program not can exit with no error
*/
System.out.println("-------------Received--------------- "
+ iNumBytes + " bytes");
System.out.println(new String (abDataIn, 0, 0, iNumBytes));
System.out.println("------------------------------------\n");
}
} catch (Exception exception) {
System.out.println ("Clnt: Exception interacting with socket: " + exception);
} finally {
try {
isIn.close();
psOut.close();
sock.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}