• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing request Parameter using Sockets

 
Naveen Kamrudeen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi How can I pass request parameter/Query String using Sockets.

Naveen
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use URL class to do a GET with parameters encoded on the URL, eg http://mysite.com/page?name=value&name=value. It's harder to do PUT. I'd look at HttpUrlConnection to do that.
 
lak naidu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,
The link that you sent does not work. Could you please send the working URL. Thank you
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch. That should have been yoursite.com not mysite.com.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java 5.0 , you can use java.util.Scanner to read data from URL .

 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by somkiat puisungnoen:
String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next();


Does this mean that we have to specify the delimeter to get the parameters using Sockets? Normally they are delimited by ampersand sign "&", aren't they?

Could you elaborate a bit on this? Thanks...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\\Z as a delimiter to Scanner (nothing to do with URL at all) is a cute trick. It means split up the stream you get in every time you find an end of stream. There is only one end of stream at, um, the end of the stream, so you get the whole contents of the stream into one string in one line of code without a read loop. Very cool.

Back to the original question ... 1) You can get content from a web site with pure sockets. It's a fair bit of work because you have to talk the HTTP protocol to the server, which means sending and reading headers and stuff. Certainly possible, maybe an entertaining way to explore HTTP. 2) You can get content from a web site much more easily with URL or HttpURLConnection because they do the hard HTTP work for you. You give the full URL including parameters ?name=value&namevalue just like you'd type into a browser. 3) Scanner is way cool to read the whole response in one big gulp.

 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Scanner is way cool to read the whole response in one big gulp.

I didn't know that Tiger's Scanner cando things like that.

Well, I should give some time to learn those cool features... If you guys got any great links about Scanner, please share here so that other people may benefit as well...

Thanks...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!