How do I retrieve a URL from a web site using Java?
-
I want to use HTTP GET and POST commands to retrieve URLs from a website and parse the HTML. How do I do this?
-
Answer:
You can use http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html in combination with http://java.sun.com/javase/6/docs/api/java/net/URL.html. URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); InputStream stream = connection.getInputStream(); // read the contents using an InputStreamReader
Johnny Maelstrom at Stack Overflow Visit the source
Other answers
The easiest way to do a GET is to use the built in java.net.URL. However, as mentioned, httpclient is the proper way to go, as it will allow you among others to handle redirects. For parsing the html, you can use http://htmlparser.sourceforge.net/.
kgiannakakis
The ticked/approved answer for this is from robhruska - thank you. This shows the most basic way to do it, it's simple with an understanding of what's necessary to do a simple URL connection. However, the longer term strategy would be to use http://hc.apache.org/httpcomponents-client/index.html for more advanced and feature rich ways to complete this task. Thank you everyone, here's the quick answer again: URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); InputStream stream = connection.getInputStream(); // read the contents using an InputStreamReader
Johnny Maelstrom
Nick Holt
I have used http://jtidy.sourceforge.net/ in a project and it worked quite well. A list of other parsers is http://java-source.net/open-source/html-parsers, but besides from JTidy I don't know any of them.
Markus
This one is set up for my drigg site This is easy to achieve using java inside your (href="JAVA CODE HERE") tag. href="javascript:window.open('http://dedlines.com/node/add/drigg/?&url='+escape(location.href), 'newwindow', config='height=600, width=500, toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=yes, directories=no, status=yes')" Simply copy the code place it inside your webpage and replace my link and form name tag with yours. Put this on the page you want to add then click it, or simply drag and drop it too your tool bar for easy link submission to your directories or bookmarking sites.
A1SURF
Related Q & A:
- How can I retrieve a deleted pic/video from my digital camera?Best solution by easeus.com
- How do I do a shot with my web cam?Best solution by answers.yahoo.com
- How can I retrieve a conversation?Best solution by Stack Overflow
- How do I change a URL for?Best solution by Stack Overflow
- How can I get free traffic to my web site?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.