1. Establish an HTTP connection (Send request parameters through get)
Require "open-Uri" # Write the GET Request Parameters in the uri url uri = 'HTTP: // URI 'html_response = nil open (URI) do | HTTP | html_response = http. read end puts html_response
2. Send request parameters through post
Params = {} Params ["name"] = 'Tom 'uri = Uri. parse ("http: // URI") RES = net: HTTP. post_form (Uri, Params) # The returned cookie puts res. header ['set-cookies'] # returned html body puts res. body
3. https-verify the server only
Require 'net/HTTPS 'require 'url' uri = Uri. parse ('https: // liuwm-PC: 8081/2. HTML ') HTTP = net: HTTP. new (URI. host, Uri. port) HTTP. use_ssl = true If Uri. scheme = "HTTPS" # enable SSL/tlshttp. verify_mode = OpenSSL: SSL: verify_none # This is also important for HTTP. start {HTTP. request_get (URI. path) {| res | print res. body }}
4. https-two-way authentication
I started to look for pfx verification, but Google did not find it for half a day. Later, I found that pfx is also a type of PKCS12 certificate. I searched the key word PKCS12 and found the desired result ~~
In addition, when Ruby sends the client, it specifies the client. CSR and client. Key Certificates instead of pfx (which has never been found). The effect is the same as that specified by the browser.
Require 'net/HTTPS 'require 'url' uri = Uri. parse ('https: // liuwm-PC: 8081/2. HTML ') HTTP = net: HTTP. new (URI. host, Uri. port) HTTP. use_ssl = true If Uri. scheme = "HTTPS" # enable SSL/tlshttp. CERT = OpenSSL: X509: Certificate. ne (file. read ("D:/111/client. CRT ") HTTP. key = OpenSSL: pkey: RSA. new (file. read ("D:/111/client. key ")," 123456 ") # key and passwordhttp. verify_mode = OpenSSL: SSL: verify_none # This is also important for HTTP. start {HTTP. request_get (URI. path) {| res | print res. body }}
Supplement:Net: HTTP cheat sheet (manual)
August lilleaas, a ruby developer in Norway, recently compiled some sample code about how to use the net: HTTP library.
Net: HTTP is widely used in many libraries, such as John Nunemaker's httparty and Paul Dix's high-performance typhoeus. As part of the standard library, although net: HTTP does not have a simple and memorable API, it is also a good alternative.
This is the connection: https://github.com/augustl/net-http-cheat-sheet, you can download it directly, each file is the corresponding use column