Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Waiting for http response takes too long..

Thread view: 
Espen Paulsberg - 17 Apr 2004 00:42 GMT
The class below sends a simple request for my webpage and retrieves
the response. But for some reason, the while loop seems to take
forever.
If I uncomment the System.out.print((char)c), I see the response
itself come back quickly, but the program will still stall for another
ten seconds or so, even though all of the response is retrieved.
It didn't help reading the socket with a bufferedreader (or any other
kind of reader for that sake) instead.
The API states that the read() method returns -1 at the end of the
stream, but it seems that it keeps blocking waiting for input data.
How do I get around this problem?

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class Connection {

public static void main(String[] args) throws Exception{

Socket s = new Socket("www.stud.ntnu.no", 80);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeBytes("GET /~paulsber/index.html" +
HTTP/1.1\r\nHost:www.stud.ntnu.no\r\n\r\n");
dos.flush();
           
int c = 0;
StringBuffer response = new StringBuffer();
while((c = dis.read()) != -1){
 response.append((char)c);
 //System.out.print((char)c);
}
System.out.println(response);
}

}

--
Espen Paulsberg
Roedy Green - 17 Apr 2004 06:35 GMT
>Socket s = new Socket("www.stud.ntnu.no", 80);
>DataOutputStream dos = new DataOutputStream(s.getOutputStream());
>DataInputStream dis = new DataInputStream(s.getInputStream());
>dos.writeBytes("GET /~paulsber/index.html" +
>HTTP/1.1\r\nHost:www.stud.ntnu.no\r\n\r\n");
>dos.flush();

Normally you would analyse the length in the http header and read only
that many bytes. see http://mindprod.com/fileio.html for how to use
either raw sockets or the HTTP classes to do this.

The socket is kept alive for possible subsequent transmission..

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Princess Morgiah - 17 Apr 2004 09:26 GMT
> The class below sends a simple request for my webpage and retrieves
> the response. But for some reason, the while loop seems to take
[quoted text clipped - 21 lines]
> dos.writeBytes("GET /~paulsber/index.html" +
> HTTP/1.1\r\nHost:www.stud.ntnu.no\r\n\r\n");

Be sure to add a Connection: close to this - if not, the server might
default to connection: keep-alive and let you wait the specified keep-alive
timeout. Should be something like 15 seconds on Apache by default I believe.

Princess Morgiah
Roedy Green - 18 Apr 2004 00:25 GMT
>Be sure to add a Connection: close to this - if not, the server might
>default to connection: keep-alive and let you wait the specified keep-alive
>timeout. Should be something like 15 seconds on Apache by default I believe.

In HTTP 1.1 keep-alive is the default.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.