Set the User-Agent Property for a SAX Parser





5
Date Submitted Mon. Aug. 13th, 2007 12:04 AM
Revision 1 of 1
Beginner bcoe
Tags Java | RSS | SAX | XML
Comments 0 comments
From a tutorial on building an RSS SAX parser on my website:

full article can be found here


        /**
        Returns an HTML representation of the news feed being
        parsed.
        */

        public synchronized String parse(){
                try{
                        XMLReader xr = XMLReaderFactory.createXMLReader();
                        xr.setContentHandler(this);
                        xr.setErrorHandler(this);
                        URL u=new URL(Url);
                        URLConnection UC=u.openConnection();
                        /*If we don't set the user-agent property sites like
                          Google won't let you access their feeds.*/

                        UC.setRequestProperty ( "User-agent", "www.plink-search.com");
                        InputStreamReader r = new InputStreamReader(UC.getInputStream());
                        xr.parse(new InputSource(r));   
                }catch(Exception e){
                }
                //Output all the parsed news items as HTML.
                for(int i=0;i<News.size();i++){
                        output+="<div class=\"search"+(i%2)+"\">";
                        output+=((NewsItem)News.get(i)).toString();
                        output+="</div>";
                }
                return(output);
        }
 

Comments

There are currently no comments for this snippet.

Voting

Votes Down