# Following url redirect

**URL:** <https://www.ruby-forum.com/t/following-url-redirect/164620>\
**Category:** Ruby\
**Created:** [April 3, 2009, 8:11am UTC](https://www.ruby-forum.com/t/following-url-redirect/164620 "2009-04-03T08:11:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [April 3, 2009, 8:11am UTC](https://www.ruby-forum.com/t/following-url-redirect/164620/1 "2009-04-03T08:11:56Z")

</div>

Hi all,  
I’m using net/http in order to scrap html contents of different  
websites. As a preliminary requirement I also want to scrap the html  
data of the redirecting urls too. For that I used the following code

response = Net::HTTP.get\_response(URI.parse(url))  
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

Now I have a doubt whether this code snippet will completely solve the  
problem in the case of url redirections. I want to solve the problem  
completely without any problem. Can somebody please tell me whether the  
above code snippet will be suitable for the issue or whether I have to  
add some additional functionalities to it. If so how??

Thanks  
Sunny
