How can I Parse an HTML response without response

2019-07-23 12:48发布

How can I grab an HTML response when there are no response headers?

I've got this:

require 'hpricot'
require 'open-uri'

doc = Hpricot(open('http://192.168.100.1/phy.htm'))

The server in this case is a cable modem that is not returning HTTP Response headers.

The above code is failing with:

C:/Ruby/lib/ruby/1.8/net/http.rb:2022:in `read_status_line': wrong status line: "" (Net::HTTPBadResponse)

from C:/Ruby/lib/ruby/1.8/net/http.rb:2009:in `read_new'

from C:/Ruby/lib/ruby/1.8/net/http.rb:1050:in `request'

from C:/Ruby/lib/ruby/1.8/open-uri.rb:248:in `open_http'

标签: ruby http
2条回答
一夜七次
2楼-- · 2019-07-23 13:36

I see a similar problem and am reluctantly coming to the same conclusion. I'm talking to a device that returns raw XML without bothering to wrap it in an HTML response.

There is a work around:

resultxml = %x{wget -qO- "http://192.168.100.1/state.xml"}
查看更多
欢心
3楼-- · 2019-07-23 13:43

So if your server is not HTTP compliant, maybe you should drop HTTP idea altogether and work with Socket directly...

Then you can use hpricot with a string returned.

查看更多
登录 后发表回答