how can i read from a socket.getinputstream?!

M

mike

outSocket.getInputStream transmit back a "http header" and a HTML
entity,
in the following java code,i plan to save the "http header" in the
file
"tidyHTTPHeader.txt" and save the HTML entity in the file
"tidytemp.html"

i run the code , i find that i get the 2 file
tidyHTTPHeader.txt,and

tidytemp.html.but strange to say,my code cannot run to the line

:System.out.println("end while");seems that the program cannot run
out

of the while loop

any suggestions?...


thanks a lot










----------------------------------------------------------------------------


InputStream in = outSocket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader
((InputStream) in));
FileWriter fw = new FileWriter("tidytemp.html");
FileWriter fwHTTPHeader= new FileWriter("tidyHTTPHeader.txt");


boolean headerflag=true;
boolean HTMLflag=false;

String line;
while((line=br.readLine()) != null) {

// line=br.readLine();
if(line.length()!=0 && headerflag==true && HTMLflag==false){
fwHTTPHeader.write(line+"\r\n");
}
else if(line.length()==0 && headerflag==true && HTMLflag==false){
fwHTTPHeader.write(line+"\r\n");
headerflag=false;
HTMLflag=true;
fwHTTPHeader.close();
}
else if(line.length()==0 && headerflag==false && HTMLflag==true){
fw.write(line+"\r\n");
}
else {
fw.write(line+"\r\n");
}
System.out.println("test");

}
System.out.println("end while");
 
G

Gordon Beaton

but strange to say,my code cannot run to the line

System.out.println("end while");

seems that the program cannot run out of the while loop.
any suggestions?...

Please format your code before posting it!

Did you read my previous response? You will not reach EOF (and exit
the loop) until the remote server *closes* his connection.

If you are using HTTP/1.1, the server keeps the connection open by
default so that you can make more requests without opening a new
connection for each one.

You can do one of two things:

- use a different condition for exiting the loop. For example, you
could use the Content-length header returned by the server to detect
the end of the response.

- you can specify Connection: close in your request header, so that
the server closes the connection after sending the response.

Read more here:

http://www.w3.org/Protocols/HTTP/1.1/rfc2616.txt

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top