Read contents of a web page  
Author Message
JRad





PostPosted: 2004-11-22 7:27:00 Top

java-programmer, Read contents of a web page I need to read the entire contents of any webpage, and return it as a
String. The other part of my assignment was to read a file and return its
contents. I did that with the following code:


private String filename;
private String contents;
public TextFileReader(String aFileName){

filename = aFileName;

}

public String readText() throws IOException{

String lineSep = System.getProperty("line.separator");
BufferedReader input = new BufferedReader(new
FileReader(filename));
String nextLine = " ";
StringBuffer contents = new StringBuffer();

while((nextLine = input.readLine()) != null){
contents.append(nextLine);
contents.append(lineSep);
}

return contents.toString();

}

----------

So, what do I have to change to be able to read webpages?

 
SMC





PostPosted: 2004-11-22 16:37:00 Top

java-programmer >> Read contents of a web page On Sun, 21 Nov 2004 18:27:12 -0500, JRad wrote:

> I need to read the entire contents of any webpage, and return it as a
> String. The other part of my assignment was to read a file and return its
> contents. I did that with the following code:

http://www.google.com.au/search?hl=en&lr=&oi=defmore&q=define:Homework

With that said, have a look at:

http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html
http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html


Sean
 
Fahd Shariff





PostPosted: 2004-11-22 18:42:00 Top

java-programmer >> Read contents of a web page Use the following:

import java.net.*;

URL url= new URL("http://www.yahoo.com") ;
BufferedReader input = new BufferedReader(new
InputStreamReader(url.openStream()));

 
 
Oscar kind





PostPosted: 2004-11-23 2:11:00 Top

java-programmer >> Read contents of a web page JRad <email***@***.com> wrote:
> I need to read the entire contents of any webpage, and return it as a
> String. The other part of my assignment was to read a file and return its
> contents. I did that with the following code:

<cut: code>

> So, what do I have to change to be able to read webpages?

Others have already shown (ways to) a solution, but didn't state the
common ground: Both files and contents of an URL (web page) can be read
as byte/character streams. Once you can handle one, you can handle the
other.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 
 
Kuldeep





PostPosted: 2006-10-24 19:50:00 Top

java-programmer >> Read contents of a web page Hi All,

I am trying to read the contents of a page through its URL.

My code snippet is as follows:
public void mtdGetPageDataHWR()
{
HttpWebRequest objRequ =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
HttpWebResponse objResp = (HttpWebResponse)objRequ.GetResponse();
string strVersion = objResp.ProtocolVersion.ToString();
StreamReader objRd = new StreamReader(objResp.GetResponseStream());
string strRd = objRd.ReadLine();
while(strRd!=null)
{
Response.Write(strRd);
strRd = objRd.ReadLine();
}
}

Is there any other way to achieve this which could be more efficient or
faster than this.

Any help on this would be very handy

Thanks,

Kuldeep


 
 
Chris Fulstow





PostPosted: 2006-10-24 20:45:00 Top

java-programmer >> Read contents of a web page Yes, check out my blog post about loading and parsing HTML using the
Html Agility Pack:
http://chrisfulstow.blogspot.com/2006/10/parsing-html-in-net.html

Kuldeep wrote:
> Hi All,
>
> I am trying to read the contents of a page through its URL.
>
> My code snippet is as follows:
> public void mtdGetPageDataHWR()
> {
> HttpWebRequest objRequ =
> (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
> HttpWebResponse objResp = (HttpWebResponse)objRequ.GetResponse();
> string strVersion = objResp.ProtocolVersion.ToString();
> StreamReader objRd = new StreamReader(objResp.GetResponseStream());
> string strRd = objRd.ReadLine();
> while(strRd!=null)
> {
> Response.Write(strRd);
> strRd = objRd.ReadLine();
> }
> }
>
> Is there any other way to achieve this which could be more efficient or
> faster than this.
>
> Any help on this would be very handy
>
> Thanks,
>
> Kuldeep