ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Net::HTTPHeader로 웹 요청하기
    Programing/Ruby 2012. 11. 20. 15:27

    참고

    http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html

    http://www.rubyinside.com/nethttp-cheat-sheet-2940.html


    GET
    require "net/http"
    require "uri"
    
    uri = URI.parse("http://b10s.org")
    
    # Will print response.body
    Net::HTTP.get_print(uri)
    
    # Shortcut
    response = Net::HTTP.get_response(uri)
    print response.body
    
    # Full
    http = Net::HTTP.new(uri.host, uri.port)
    response = http.request(Net::HTTP::Get.new(uri.request_uri))
    print response.body
    

    POST
    require "net/http"
    require "uri"
    
    uri = URI.parse("http://b10s.org")
    
    # Shortcut
    response = Net::HTTP.post_form(uri, {"id" => "J4ckP4rd", "pw" => "password"})
    
    # Full control
    http = Net::HTTP.new(uri.host, uri.port)
    
    request = Net::HTTP::Post.new(uri.request_uri)
    request.set_form_data({"id" => "J4ckP4rd", "pw" => "password"})
    
    response = http.request(request)
    

    Send Custom Headers
    require "net/http"
    require "uri"
    
    url = URI.parse("http://b10s.org")
    
    req = Net::HTTP::Get.new(url.path)
    req.add_field("X-Forwarded-For", "0.0.0.0")
    
    res = Net::HTTP.new(url.host, url.port).start do |http|
      http.request(req)
    end
    
    puts res.body
    


    'Programing > Ruby' 카테고리의 다른 글

    Ruby & Ruby on rails 설치  (0) 2012.11.16
Designed by Tistory.