Ruby:Net :: HTTP解析最后添加%0A

时间:2016-12-26 08:59:07

标签: ruby net-http

我试图打开一个名为test.txt的文件,然后在名为' line'的变量中导入每一行。然后将其添加到url文件中以发出请求。

require 'net/http'
require 'uri'

puts "Enter URL :-"
gurl = gets.chomp

total = File.foreach('test.txt').count
c=0

while(c < total)
line = IO.readlines("test.txt")[c]
aurl = "#{gurl}/#{line}"
encoded_url = URI.encode(aurl)
url = URI.parse(encoded_url)
puts "\n #{url} \n"
res = Net::HTTP.get_response(url)
puts "\n Response received for #{line} => #{res.code} \n"
c+=1
end

示例outpute =&gt;

 http://www.example.com/z.php%0A

 Response received for z.php
 => 404

 http://www.example.com/index.php%0A

 Response received for index.php
 => 404

无论如何要逃脱%0A(换行符)? 全新的红宝石

2 个答案:

答案 0 :(得分:1)

如果gitlab.example.com/new_site/有n行,你会读n + 1次!

这是一个修改后的版本,它会删除换行符/换行符:

test.txt

答案 1 :(得分:0)

您可以使用chomp删除换行符:

line = IO.readlines("test.txt")[c].chomp