This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes Need to see how HTTP Works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need to see how HTTP Works" Watch "Need to see how HTTP Works" New topic
Author

Need to see how HTTP Works

Mohan Panigrahi
Ranch Hand

Joined: Sep 28, 2001
Posts: 142
I have written a URL connection program, which would request a webpage from the server. I find that HTTP headers are missing from the the text that I get. Could anyone suggest what can I write more in this program so that I can get to see

HTTP/1.1 200 OK
Content-Type: tex/html

apart from the usual <html> tags.

My program
==========



Request for Moderators: Please don't move it in HTML forums, because it is servlet programmers who deal with getting and setting of headers and not HTML programmers.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
The URLConnection.getHeaderFields() method lets you access all headers.


Ping & DNS - updated with new look and Ping home screen widget
Mohan Panigrahi
Ranch Hand

Joined: Sep 28, 2001
Posts: 142
Hi Ulf ,
Thanks very much. Would you know how can I obtain everything, the headers, status message and the actual html thing by a single stream. May be I need to use some type of socket connection?

Thanks,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
If you use Jakarta HttpClient instead of java.net.HttpUrlConnection, there's a class called HttpConnection which gives you access to the raw streams. Maybe that allows you to do that. I've never done it, so I can't help with that.

Added later: Looking at the URLConnection API, it does have getInputStream and getOutputStream methods, which may help you accomplish what you're trying to do.
[ August 14, 2005: Message edited by: Ulf Dittmer ]
Mohan Panigrahi
Ranch Hand

Joined: Sep 28, 2001
Posts: 142
Thanks Ulf,
Regarding your second suggestion, that is exactly what I have used in the program, but it does not give the full text ( including headers etc.)

But, I would not like to use some ready made HTTPClient. I would have liked to do the raw Java way.....thanks anyways for your help....

Thanks,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I attempted to reproduce a subset of HttpURLConnection using raw sockets because I wanted control over the socket timeout. It was entertaining, but a pretty big task. To do it you'd have to send all the headers and the request, read and parse all the headers and the response. Starting with the source for the HttpClient version would be a big jump start. Some place down inside it must do exactly that ... read the socket stream and interpret everything that comes through. Maybe you can just insert some hooks and make it call a listener for each header.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410


But, I would not like to use some ready made HTTPClient. I would have liked to do the raw Java way.....thanks anyways for your help....

Sounds like you're looking for this:
http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html

Socket programming is very different from Servlet coding, so this definately won't be the place to go for help if you get stuck.

This would:
http://www.coderanch.com/forums/f-8/sockets
[ August 14, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Harish M
Greenhorn

Joined: Aug 13, 2005
Posts: 8
If your interest is to just understand the flow of headers, you can also use external tools like Ethereal,Paros etc which are good sniffers.This will help you to narrow down each and every bit transferred !!!

Harish
Babu Rajendra Prasath Haridass
Greenhorn

Joined: Jan 17, 2005
Posts: 3
Hi Mohan Panigrahi ,

using this code you can get the Header information.


[Edited by Dave. Added code tags and reduced leading whitespace]
[ August 19, 2005: Message edited by: David O'Meara ]

SCJP 1.4, SCWCD 1.4, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to see how HTTP Works
 
Similar Threads
how to read content of a site?
URLConnection returning html output instead of XML
being stuck at readLine() when reading HTTP request
How to specify the name of the database
GET html contents from a web server