Ruby sends HTTP protocol (get, post, https server verification, https two-way authentication)

Source: Internet
Author: User
Tags pkcs12

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.