Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add http client ruby script

  • Loading branch information...
commit 02b50c97dc5acfd476ee78c1c6fe370502589fd9 1 parent ab30197
@guiwuu authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 ruby/howto/http_client.rb
View
31 ruby/howto/http_client.rb
@@ -0,0 +1,31 @@
+require 'net/http'
+
+Net::HTTP.start('google.com.hk', 80) do |http|
+ path1 = 'http://www.taobao.com/'
+ path2 = 'http://www.taobao.com/'
+
+ # get
+ response = http.get(path1)
+ puts response.code
+
+ # post
+ params={"#q" => '123'}
+ uri= URI.parse(path1)
+ response = Net::HTTP.post_form(uri, params)
+ puts response.code
+
+ # cookie
+ all_cookies = response.get_fields('set-cookie')
+ cookies_array = Array.new
+ all_cookies.each { |cookie|
+ cookies_array.push(cookie.split('; ')[0])
+ }
+ cookies = cookies_array.join('; ')
+ headers = {
+ 'Cookie' => cookies
+ }
+
+ # header
+ response = http.get(path2, headers)
+ puts response.code
+end
Please sign in to comment.
Something went wrong with that request. Please try again.