Module: Request

Defined in:
lib/core_blur/util/request.rb

Class Method Summary collapse

Class Method Details

.get(proxy_ip, proxy_port, url) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/core_blur/util/request.rb', line 20

def Request.get(proxy_ip, proxy_port, url)
    headers = {
        "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
        "Accept-Encoding" => "gzip"
    }
    options = {
        :headers => headers,
        :http_proxyaddr => proxy_ip,
        :http_proxyport => proxy_port
    }
    begin
        response = HTTParty.get(url, options)
        puts "响应状态码:#{response.code}"
        if response.body.nil? || response.body.empty?
            puts "page content: #{response.body}"
            error = "请求失败,status code: #{response.code.to_s}"
            puts "❌ #{error}"
            Process.exit(-1)
        else
            return response
        end
    rescue => exception
        puts "\n"
        puts caller
        puts "调用堆栈如上\n\n"
        error = "请求失败, 请检查网络或参数是否正确:#{exception}"
        puts "❌ #{error}"
        Process.exit(-1)
    end
end

.post(url, params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/core_blur/util/request.rb', line 8

def Request.post(url, params)
    uri = URI(url)
    http = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == "https"
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    header = {'content-type':'application/json'}
    response = http.post(uri, params.to_json, header)
    puts response.body
    response.body
end