zoukankan      html  css  js  c++  java
  • http模拟登录

    = =其实很简单,写这个的目的呢,是为了练习下Ruby而已

    就是post到登录地址,得到cookie,然后以后的请求都用这个cookie就好了。

     1 require 'net/http'
     2 module EasyHTTP
     3     class HTTP
     4         def initialize(url)
     5             @url = url
     6             @cookie = nil
     7         end
     8         def login
     9             uri =  URI.parse(@url)
    10             params = {}
    11             params['user.username'] = '1111@xx.com'
    12             params['user.password'] = 'xxxx'
    13             res = Net::HTTP.post_form(uri , params)
    14             @cookie = res.header['set-cookie'].split('; ')[0]
    15         end
    16         def get(url)
    17             login unless @cookie
    18             uri = URI.parse(url)
    19             http = Net::HTTP.new(uri.host , uri.port)
    20             request = Net::HTTP::Get.new(uri.path)
    21             request['Cookie'] = @cookie
    22             res = http.request(request)
    23             res.body
    24         end
    25     end
    26 end
    27 
    28 sample = EasyHTTP::HTTP.new("http://loginAction url")
    29 puts sample.get("http://some url u want")
  • 相关阅读:
    [TCP/IP]TCP的三次握手和四次挥手

    思考
    jQuery完整的事件委托(on())
    jQuery队列动画
    jQuery自定义动画
    jQuery淡入淡出
    jQuery滑动动画
    jQuery基本动画
    jQuery基础3
  • 原文地址:https://www.cnblogs.com/x1957/p/3286997.html
Copyright © 2011-2022 走看看