1, 用Net::HTTP.get
uri = URI('http://xxxxxxx.com/api/user/1.0/json/get_info?&user_id=3') Net::HTTP.get(uri)
2, 用Net::HTTP.get 時加一些參數(shù)
uri = URI('http://xxxxxx.com/api/user/1.0/json/get_info') params = { :user_id => 3} uri.query = URI.encode_www_form(params) res = Net::HTTP.get_response(uri) puts res.body if res.is_a?(Net::HTTPSuccess)
3, Net::HTTP.post_form
uri = URI('http://xxxxx.com/api/user/1.0/json/login') res = Net::HTTP.post_form(uri, 'client_id' => '159d8a2f931514c107280', 'identity' => 'xxxxx002','password' => 'xxxxxx') puts res.body
4, res.methods里面有些很有用的方法
uri = URI('http://drhel31.sh.magima.com/api/user/1.0/json/get_info?&user_id=3') res = Net::HTTP.get_response(uri) # Headers res.header # => "#<Net::HTTPOK 200 OK readbody=true>" res.read_header # => "#<Net::HTTPOK 200 OK readbody=true>" # Status 這個很有用,做接口調用成功與否的判斷可用。 puts res.code # => '200' puts res.message # => 'OK' puts res.class.name # => 'HTTPOK' res.code_type # => Net::HTTPOK res.inspect # => "#<Net::HTTPOK 200 OK readbody=true>" # Body puts res.body if res.msg == "OK"
5, 判斷文件是否存在
File.exist? '/home/test/features/support/modul_login.rb'
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁標題:Ruby學習筆記系列(一)-創(chuàng)新互聯(lián)
當前網(wǎng)址:http://chinadenli.net/article46/deiheg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、網(wǎng)站策劃、移動網(wǎng)站建設、動態(tài)網(wǎng)站、網(wǎng)站收錄、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容