开发者

Convert to string/text ruby

I'm using the following code to download a page through a POST request:

require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'开发者_如何学JAVApost'=>'1'})
puts res.split("Date")

The URL I originally used has been replaced with example.com

It works great, but when I try to call split (last line) it returns an error:

<main>': undefined methodsplit' for # (NoMethodError)

I'm new to ruby, so I'm confused about this error.


The method you are calling returns a HTTPResponse object, so you need to leverage that object's methods to get what you want. maybe something like:

require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'post'=>'1'})
puts res.body.split("Date")

Notice the body method.

Or, if you want to see all the data returned:

require 'net/http'
require 'uri'
res = Net::HTTP.post_form(URI.parse('http://example.com'),{'post'=>'1'})
puts res.inspect

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜