2012-03-12 11 views
16

यह महान काम करता है:प्राप्त HTTPS प्रतिक्रिया

require 'net/http' 

uri = URI('http://api.twitter.com/1/statuses/user_timeline.json') 
args = {include_entities: 0, include_rts: 0, screen_name: 'johndoe', count: 2, trim_user: 1} 
uri.query = URI.encode_www_form(args) 
resp = Net::HTTP.get_response(uri) 
puts resp.body 

लेकिन http से https के लिए बदल रहा एक अर्थहीन त्रुटि की ओर जाता है। मैं क्यों त्रुटि व्यर्थ है, मैं सिर्फ पता है कि https के लिए get_response कर के निकटतम साधन है चाहते हैं नहीं पूछ रहा हूँ?

मैं 'https' Net::HTTP दस्तावेज़ में उदाहरण देखा है लेकिन यह बहुत प्रभावशाली नहीं लग रहा है और मुझे हस्तचालित रूप से अपने मापदंडों हैश से URL रचना कर देगा - अच्छा नहीं।

उत्तर

21

यहाँ एक उदाहरण है जो मेरे लिंक पर रूबी 1.9.3 के तहत मेरे लिए काम करता

require "net/http" 

uri = URI.parse("https://api.twitter.com/1/statuses/user_timeline.json") 
args = {include_entities: 0, include_rts: 0, screen_name: 'johndoe', count: 2, trim_user: 1} 
uri.query = URI.encode_www_form(args) 
http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true 

request = Net::HTTP::Get.new(uri.request_uri) 

response = http.request(request) 
response.body 
+1

डॉक है माणिक के पिछले संस्करणों आप HTTPS का उपयोग करने के लिए 'नेट/https' की आवश्यकता होती है की आवश्यकता होगी _In कहते हैं। यह अब true._ है - हम सिर्फ इस पर ध्यान न दें चाहिए? –

+0

तुम ठीक कह रहे, की आवश्यकता होती है "शुद्ध/http" ठीक है, मैं इस सवाल का जवाब – beanie

+1

संपादित करेंगे दस्तावेज़ भी कहते हैं _require 'शुद्ध/http' भी 'URI' तो आप इसे –