954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HTTP GET and POST REQUEST

Dear all I am currently trying to find a java source code with which either HTTP POST or GET request can be sent. Unfortunately I have run into bunch of snippets that I can not run. Is there anyone that can tell me where I can find complete source code for sending POST or GET request?
Thank you in advance

rinko
Newbie Poster
12 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

What are you talking about?

Look up HttpURLConnection in the API or HttpClient from apache. And, no, neither of these things will give you finished code to simply execute and do what you want. Nothing ever will.

Both of these are APIs, of which you can search and find tutorials for, but you need to use them in your coding in order to do what you want. Trust me, you can do both a Get and a Post with either of them.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thank you for your help. I used HttpUrlConnection and I managed to establish the connection with a server. Now, I would like to see the content of the response. With getResponseCode and getResponseMessage I only get responses on my request but not the content. What class should be used in this case?

rinko
Newbie Poster
12 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

If you are talking about reading the contents of the form submitted using the GET or POST method, try looking into getParameter(input_type_name) method of the request class.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

I am talking about the contents of the response. What if response contains an image, XML file, or a hyperlink for example. How can I read these content elements?
Thanks

rinko
Newbie Poster
12 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

You don't as such get the contents of the resopnse. Reponse in general is sent to the client -- what to do with it depends on the client, which is normally browser in our case. You can let the browser know you are sending an XML file or video by setting the content type of the response.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

You need to call getInputStream(), to get the inputStream of the response (actually, in relation to the response it is an outputStream, but in relation to your Java Program it is an inputStream), then you can read that, but remember, it will be HTML, so any images will be found as IMG tags, then you then need to retrieve with another HttpURLConnection and its getInputStream.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

As the Input stream I get:
sun.net. www.protocol.http .
HttpURLConnection
[EMAIL="$HttpInputStream@186c6b2"]$HttpInputStream@186c6b2[/EMAIL]
How can I get behind this 186c6b2 part?

rinko
Newbie Poster
12 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Read the Sun I/O Tutorials to find out how you work with an InputStream.

An InputStream is NOT a String representation of the entire page contents. It is a "Stream" that you have to read byte by byte (although wrapping it in other objects, i.e. a BufferedReader, will allow you to read it line by line, rather than byte by byte). But that is too much to explain here, so please read the I/O Tutorials at Sun.

http://java.sun.com/docs/books/tutorial/essential/io/index.html

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

How can I go about when as an reply I get a byte stream that represents a gif file?

rinko
Newbie Poster
12 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

The ImageIO method read(InputStream) produces a BufferedImage. Read the API for tht and try it out.

Edit:

Of course, ImageIO also has a read(URL) method that also returns a BufferedImage. So, rather than reading the link (URL) found in the src attribute of the IMG tag, you could simply feed that link directly to ImageIO.read Check that out as well, as it will probably save you time.

And always remember, the API is your friend.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Can you please send me the code that you have written to send a GET request to the server from client. I am in ba need of that code. Thank you very much in advance.

madhavireddy
Newbie Poster
2 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You