Class: QQBot::Client
- Inherits:
-
Object
- Object
- QQBot::Client
- Defined in:
- lib/qqbot/client.rb
Constant Summary collapse
- @@user_agent =
'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'
Class Method Summary collapse
Instance Method Summary collapse
- #get(uri, referer = '') ⇒ Object
- #get_cookie(key) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #post(uri, referer = '', form_data = {}) ⇒ Object
Constructor Details
Class Method Details
.origin(uri) ⇒ Object
9 10 11 |
# File 'lib/qqbot/client.rb', line 9 def self.origin(uri) "#{uri.scheme}://#{uri.host}" end |
Instance Method Details
#get(uri, referer = '') ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/qqbot/client.rb', line 17 def get(uri, referer = '') QQBot::LOGGER.debug { "get #{uri.to_s}" } Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| req = Net::HTTP::Get.new(uri) req.initialize_http_header( 'User-Agent' => @@user_agent, 'Cookie' => @cookie.to_s, 'Referer' => referer ) res = http.request(req) @cookie.put(res.get_fields('set-cookie')) QQBot::LOGGER.debug { "code: #{res.code}, body: #{res.body}" } return res.code, res.body end end |
#get_cookie(key) ⇒ Object
56 57 58 |
# File 'lib/qqbot/client.rb', line 56 def (key) @cookie[key] end |
#post(uri, referer = '', form_data = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/qqbot/client.rb', line 36 def post(uri, referer = '', form_data = {}) QQBot::LOGGER.debug { "post uri: #{uri.to_s} data: #{form_data.to_s}" } Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| req = Net::HTTP::Post.new(uri) req.set_form_data(form_data) req.initialize_http_header( 'User-Agent' => @@user_agent, 'Cookie' => @cookie.to_s, 'Referer' => referer, 'Origin' => self.class.origin(uri) ) res = http.request(req) @cookie.put(res.get_fields('set-cookie')) QQBot::LOGGER.debug { "response code: #{res.code}, body: #{res.body}" } return res.code, res.body end end |