Win a copy of Getting started with Java programming language this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

stuck building my own simple HTTP client  RSS feed

 
C Johnston
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to send a proper HTTP header to any server and receive a request but not allowed to use and of the built in java classes....

I have this that I need to send to a server and print a response but Im stuck...

GET / HTTP/1.0
Host: google.com
Connection: close
Header: Value
Empty line


this is what I have so far:
Any tips at all would be great, something about the input reading until an empty line then sending is messing me up


 
Carey Brown
Saloon Keeper
Posts: 2453
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to create a socket connection, and then using that to create two threads, one for getting user input and writing it to the socket, and the other to read from the socket and write the output to the console.

Without threads, as you have done, your calls to readLine() block each other.
 
Carey Brown
Saloon Keeper
Posts: 2453
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will never be true because readLine() strips off the new-line character(s).
 
C Johnston
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You'll need to create a socket connection, and then using that to create two threads, one for getting user input and writing it to the socket, and the other to read from the socket and write the output to the console.

Without threads, as you have done, your calls to readLine() block each other.


Aren't inFromUser and inFromServer two separate threads?
 
C Johnston
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:This will never be true because readLine() strips off the new-line character(s).


Do you know a way to have this run until theres a blank line? (e.g. pressing enter without typing anything)
 
Carey Brown
Saloon Keeper
Posts: 2453
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chhris Jjohnston wrote:
Carey Brown wrote:You'll need to create a socket connection, and then using that to create two threads, one for getting user input and writing it to the socket, and the other to read from the socket and write the output to the console.

Without threads, as you have done, your calls to readLine() block each other.

Aren't inFromUser and inFromServer two separate threads?

You have two BufferedReader instances that are both running in the current thread. "readLine()" will block (halt) the thread execution until data is available. If you want a separate thread you must design a class derived from the "Thread" class then create an instance of that and start() it.

There are several tutorials on Java concurrency and threads.
http://docs.oracle.com/javase/tutorial/essential/concurrency/procthread.html

As an alternative for your specific needs you could create a String array with the lines you want to send, and then loop through them writing them with "\r\n" out to the socket. Only after you've done that, loop calling readLine() on the socket to get the returned results. In this way you are not intermingling readLine() calls.
 
Carey Brown
Saloon Keeper
Posts: 2453
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chhris Jjohnston wrote:
Carey Brown wrote:This will never be true because readLine() strips off the new-line character(s).


Do you know a way to have this run until theres a blank line? (e.g. pressing enter without typing anything)



Note that works just like
but is more resilent to the case where "request" reference might accidentally be null.
 
Damon McNeill
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chhris Jjohnston wrote:Need to send a proper HTTP header to any server and receive a request but not allowed to use and of the built in java classes....


I wonder why? Is it just some arbitrary assignment? There's libraries that will do this and more, already developed. No need to reinvent the wheel.
 
C Johnston
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damon McNeill wrote:
Chhris Jjohnston wrote:Need to send a proper HTTP header to any server and receive a request but not allowed to use and of the built in java classes....


I wonder why? Is it just some arbitrary assignment? There's libraries that will do this and more, already developed. No need to reinvent the wheel.



Yep, dumb assignment.. Still struggling
 
Damon McNeill
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not dumb. I'm sure there's a context-free grammar that specifies what a valid HTTP header look like. Your problem is to generate valid instances of that grammar.

 
Damon McNeill
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The code here seems to assume that a "request" is fully contained in one line; however, an HTTP request may contain multiple newlines. This is not a simple problem. I think to get a correct solution, would require you to write a HTTP request parser, and a generator, according to the formal grammar of HTTP. It would be sort of similar to writing a JSON parser,  but HTTP is a lot simpler than JSON.
 
Damon McNeill
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, given the HTTP header as

GET / HTTP/1.0
Host: google.com
Connection: close
Header: Value



your code is sending each line of this as a full request, which is not what is needed.
 
Damon McNeill
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read the full header, and only then, send it to the server for a response.

Hope it helps.


 
Tim Holloway
Bartender
Posts: 18512
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can actually test sending HTTP requests/headers using the telnet utility - the HTTP protocol, like most of the original Internet protocols was designed to work with plain text commands and data.

That was partly because back then, requests could end up routed to/through all sorts of different architectures (including IBM EBCDIC text), and partly because it's easier to test-drive the protocols manually before committing them to code.

The java.net services are very sophisticated. They not only can send/receive HTTP exchanges, they can even automatically and transparently manage cookies. Which is why rolling your own client only makes sense as an academic exercise.

But what actually goes over the wire, reduced to basics is simply this:

1. Command (GET/POST, etc.)
2. Header(s) optional. Cookies are headers, too!
3. 1 blank line (required if content will follow)
4. content (optional - you should include a Content-Type header to indicate what the data format is)

Which, you'll notice, follows the sequence that you outlined in detail. The blank line(3) is critical, since it's how the server knows when to stop treating the incoming data as headers and when to start treating incoming data as data (for example HTML form posts).

Finally, as I said, HTTP is a text protocol, but "text" is a loose term. In terms of this particular protocol, it's something that falls within the accepted character coding of the target system, and it's broken into single lines, which may be terminated with CR/LF or simply LF (also known as NL  for newline) control codes. The CR, is, in fact, to be ignored.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!