- This topic has 2 replies, 3 voices, and was last updated 15 years, 11 months ago by rmcvay.
-
AuthorPosts
-
Pradeep_JMemberHi ,
My aim is to retrieve data from a website using HttpURLConnection
I wrote thisimport java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;public class webPractice
{public static void main(String[] args)
{try
{
URL yahoo = new URL(“http://www.yahoo.com/”);
URLConnection yahooConnection = (URLConnection) yahoo.openConnection();
yahooConnection.connect();
long s=yahooConnection.getDate();System.out.println(“date”+(String)yahooConnection.getContent());
System.out.println(“Hi”);
}
catch (MalformedURLException e){
System.out.println(“URL Exception”);
// new URL() failed
}
catch (IOException es)
{
System.out.println(“IO Exception”);
es.printStackTrace();
// openConnection() failed
}}
}
It is giving the exception as
java.net.UnknownHostException: http://www.yahoo.com
IO Exception
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at webPractice.main(webPractice.java:19)How can I retrieve the data from a website?
If do u have any working code plz show me….
Loyal WaterMemberMoving to Off Topic >> Software Development.
rmcvayMemberSomething like this maybe.
public StringBuffer getYahoo() throws IOException, MalformedURLException { URL url = new URL("http://www.yahoo.com"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuffer inBuff = new StringBuffer(); int c; while ((c = br.read()) != -1) { inBuff.append((char)c); } return inBuff; }
-
AuthorPosts