For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2004 > Simple reading HTML from a URL problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Simple reading HTML from a URL problem
XMLSDK

2004-04-29, 2:24 pm

I have written a programme to receive the HTML code from a URL
http://www.pru.com.hk/
But I dunno why it doesn't work. It can only receive the beginning few
lines:

<!-- i n clude file="BB.asp" -->

And the remaining cannot be obtained. Can anyone help me with this problem?
Thank you.


import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class App {

public static void main(String[] args) {
BufferedReader br = null;
try
{
URL url = new URL("http://www.pru.com.hk/");
URLConnection urlConn = url.openConnection();
InputStreamReader isr = new InputStreamReader(urlConn.getInputStream());
br = new BufferedReader(isr);

String str;
while ((str = br.readLine()) != null)
{
System.out.println(str);
}
}
catch (IOException e)
{
System.err.println(e);
}
finally
{
try
{
if (br != null) br.close();
}
catch (IOException e)
{
System.err.println(e);
}
}
}
}


Fahd Shariff

2004-04-30, 1:22 pm

Try:

.....
URL url = new URL("http://www.pru.com.hk/");
URLConnection urlConn = url.openConnection();

//ADD THIS LINE:--------->
urlConn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible;
MSIE 5.5; Windows NT 5.0; H010818)" );
......

Hope it works!

Fahd
http://www.fahdshariff.cjb.net


"XMLSDK" <xmlsdk@yahoo.com.hk> wrote in message news:<40912dad$1_3@rain.i-cable.com>...
> I have written a programme to receive the HTML code from a URL
> http://www.pru.com.hk/
> But I dunno why it doesn't work. It can only receive the beginning few
> lines:
>
> <!-- i n clude file="BB.asp" -->
>
> And the remaining cannot be obtained. Can anyone help me with this problem?
> Thank you.
>
>
> import java.io.*;
> import java.net.URL;
> import java.net.URLConnection;
>
> public class App {
>
> public static void main(String[] args) {
> BufferedReader br = null;
> try
> {
> URL url = new URL("http://www.pru.com.hk/");
> URLConnection urlConn = url.openConnection();
> InputStreamReader isr = new InputStreamReader(urlConn.getInputStream());
> br = new BufferedReader(isr);
>
> String str;
> while ((str = br.readLine()) != null)
> {
> System.out.println(str);
> }
> }
> catch (IOException e)
> {
> System.err.println(e);
> }
> finally
> {
> try
> {
> if (br != null) br.close();
> }
> catch (IOException e)
> {
> System.err.println(e);
> }
> }
> }
> }

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com