Michael Rutherfurd wrote:
> I am trying to write a quick test program to display the response of
> a http request in its entirety ie including the response headers
> etc
> I'm currently using the following code but it only shows the content.
>
> URL data = new URL("http", "10.10.80.199", "/TestServlet");
> BufferedReader
> in = new BufferedReader(new InputStreamReader(data.openStream()));
> String line;
> while ((line = in.readLine()) != null)
> {
> System.out.println(line);
> }
>
> What am I not doing/understanding?
It seems to me that URL represents only the resource, not the
corresponding metadata (e.g. headers) sent by server. So if you want to
get everything the server is sending (headers + content), the sure way
is to use Socket and send the server appropriate request "manualy", eg.:
<code>
...
// get socket to standard HTTP port (80)
Socket sock = new Socket("10.10.80.199",80);
// get output
PrintStream output = new PrintStream(socket.getOutputStream());
// get input
InputStream input = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
// send request (this is HTTP 1.0)
// do not forget the last "\n" - HTTP request ends with empty line
output.println("GET /TestServlet HTTP/1.0\n");
// read response - it can be your code
String line;
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
...
</code>
> Michael Rutherfurd
Regards,
J.Ch.
--
Ing. Jozef Chocholacek Qbizm Technologies, Inc.
Chief Project Analyst ... the art of internet.
________________________________________________________________
Kralovopolska 139 tel: +420 5 4124 2414
601 12 Brno, CZ http://www.qbizm.com fax: +420 5 4121 2696
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html