2012-06-11 3 views

답변

0

XMLHTTPRequest는 브라우저 개념이지만 Ruby에 대해 묻는 중이므로 루비 스크립트에서 이러한 요청을 시뮬레이션하는 것으로 가정합니다. 이를 위해 HTTParty이라는 매우 사용하기 쉬운 보석이 있습니다. 여기

이 간단한 예제 (당신이 보석을 가지고 가정 - gem install httparty에 설치) :

require 'httparty' 
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json') 
puts response.body, response.code, response.message, response.headers.inspect 
+0

흠, 이것은 좋지만, 나는 미리 긁어 내야 할 웹 사이트에 로그인해야 할 것입니다. HTTParty는 Mechanize처럼 쉽게 할 수없는 것 같습니다. – qendu

+3

@ user1223734 좋습니다.하지만 중요한 경우 질문에 언급해야합니다. – Digitalex

2

기계화 : '순/HTTP'와

require 'mechanize' 
agent = Mechanize.new 
agent.post 'http://www.example.com/', :foo => 'bar' 
1

예, (루비 1.9.3를) :

POST 요청에 XMLHttpRequest에 대한 추가 헤더 만 넣으면됩니다 (아래 참조).

require 'net/http' 
require 'uri' # convenient for using parts of an URI 

uri = URI.parse('http://server.com/path/to/resource') 

# create a Net::HTTP object (the client with details of the server): 
http_client = Net::HTTP.new(uri.host, uri.port) 

# create a POST-object for the request: 
your_post = Net::HTTP::Post.new(uri.path) 

# the content (body) of your post-request: 
your_post.body = 'your content' 

# the headers for your post-request (you have to analyze before, 
# which headers are mandatory for your request); for example: 
your_post['Content-Type'] = 'put here the content-type' 
your_post['Content-Length'] = your_post.body.size.to_s 
# ... 
# for an XMLHttpRequest you need (for example?) such header: 
your_post['X-Requested-With'] = 'XMLHttpRequest' 

# send the request to the server: 
response = http_client.request(your_post) 

# the body of the response: 
puts response.body 

관련 문제