From: Arun Kumar Date: 2009-04-06T23:39:14+09:00 Subject: Re: Code duplication Loga Ganesan wrote: > Arun Kumar wrote: >> Hi all, >> The following is the code for extracting the html contents of a >> website. I have included the code in case url redirect and BadRequest >> error. >> >> #getting the HTTP response from 'uri' >> response = Net::HTTP.get_response(uri) >> case response >> # if the url is redirecting then fetch the contents of the >> redirected url >> when Net::HTTPRedirection then uri = URI.parse(response['Location']) >> response = >> Net::HTTP.get_response(uri) >> # in case of a bad request error >> when Net::HTTPBadRequest then http = Net::HTTP.start(uri.host, >> uri.port) >> #getting the html data by setting the path as '/' and using a user >> agent >> response = http.get("/", "User-Agent"=>"Mozilla/4.0 (compatible; MSIE >> 5.5; Windows NT 5.0)") >> end >> >> data = response.body >> >> My tutor is saying that there is a duplication in the above code. ie. >> code for html reading is specified twice without any purpose and it >> should be removed. I've no idea where there is a mistake. I'm a newbee >> to ruby and i don't understand the problem correctly or where things >> went wrong. Can anyone please help me to find the mistake. >> >> Thanks in advance. >> >> Regards >> Arun > > What is the use of this below statement ? > response = http.get("/", "User-Agent"=>"Mozilla/4.0 > (compatible; MSIE > 5.5; Windows NT 5.0)") > > Since you had already got the response object using get_response, then > why it is needed? Hi, Thanks for the reply. If it is a bad request error, then I have to communicate to the port and host and then I've to fetch the data. For eg. if i try to fetch html contents from youtube.com, i get a bad request error. So I used the Net::HTTP.start() and then I used the path and user agent to retreive the contents and stored it in response. I dont think that there is any other way. If I remove that part, I'm not able to read the html. Thanks Arun -- Posted via http://www.ruby-forum.com/.