The Net::http method in Ruby does not support persistent connections, and if you do many HTTP requests, they are not recommended; Here is a list of some fixed usages (which are still a bit uncomfortable):
One , two does not support the request header setting (header takes the Ruby default value), can only be used for basic requests; Three, four can set the request header;
Net::http cannot handle redirection and 404; session hold is not supported
A. Basic GET requestGet_response
Require'net/http'#Basic GET RequestRequire'net/http'URI= URI ('Http://httpbin.org/get') Response=Net::http.get_response (URI) puts Response.body#GET request with parametersRequire'net/http'URI= URI ('http://httpbin.org/get?name=zhaofan&age=23') Response=Net::http.get_response (URI) puts Response.body#if we want to pass data in a URL query string, we usually pass the Httpbin.org/get?key=val way#The URI module allows you to convert a hash using the Encode_www_form method, as in the following example:Require'net/http'URI= URI ('http://httpbin.org/get?') Data= {Name:'Zhaofan', age:23}uri.query=uri.encode_www_form (data) Response=Net::http.get_response (URI) puts Response.body
Two. Basic POST requestPost_form (header default is application/x-www-form-urlencoded)
' net/http ' # Basic POST Request # by adding a params parameter when sending a POST request, the params parameter can be constructed from a dictionary; # the method underlying and get_response are called the response method uri = Uri ('http://httpbin.org/post' = {name:'zhaofan', age:23= net::http.post_form (URI, params) puts Res.body
three. Get Fixed usage
Require'net/http'URL= URI ('http://httpbin.org/get?')#Set Request Parametersparams = {'name':'Test'}url.query=uri.encode_www_form (params) HTTP=net::http.new (Url.host, Url.port)#set the request headerHeader = {'user-agent':'chrome/61.0.3163.100'}response=http.get (URL, header) puts Response.body
four. Post fixed usage
- Application/json
Require'net/http'require'JSON'#Application/jsonurl = URI ('Http://httpbin.org/post') HTTP=net::http.new (Url.host, Url.port)#Set Request Parametersdata = {'Code': 1,'Sex':'male','ID': 1900,'name':'Test'}.to_json#set the request headerHeader = {'Content-type':'Application/json'}response=http.post (URL, data, header) puts Response.body
- application/x-www-form-urlencoded
Require'net/http'require'JSON'#application/x-www-form-urlencodedurl = URI ('Http://httpbin.org/post') HTTP=net::http.new (Url.host, Url.port)#encodeURI Parametersdata = Uri.encode_www_form ({'name':'Test'})#set the request headerHeader = {'Content-type':'application/x-www-form-urlencoded'}response=http.post (URL, data,header) puts Response.body
Five. Response response content
Require'net/http'URI= URI ('http://www.baidu.com') Response=Net::http.get_response (URI) puts"http version: #{response.http_version}" #= 1.1Puts"Response code: #{response.code}" #= +Puts"response information: #{response.message}" #= OKPuts"Uri:#{response.uri}" #= http://www.baidu.comPuts"decoding information: #{response.decode_content}" #= TruePuts Response.body#+ = Response BodyResponse.header.each do |k,v|puts"#{k}:#{v}"End#= = Response header
Six. Sending HTTPS requests
' Net/https ' # set HTTPSHttp.use_ssl == Openssl::ssl::verify_none
Ruby Net/http module uses