RUBY-SSL,基本身份验证和POST [英] RUBY - SSL, Basic Auth, and POST

查看:260
本文介绍了RUBY-SSL,基本身份验证和POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这方面遇到了很大的困难-似乎有一些代码片段,这些代码片段我似乎无法拼凑在一起.我只是尝试发布键/值对,但得到了Connection refused - connect(2) (Errno::ECONNREFUSED).救命!

I'm having quite a hard time with this -- seems like there are a few snippets of code lying around that I can't seem to piece together. I'm simply trying to POST key/value pairs, but getting Connection refused - connect(2) (Errno::ECONNREFUSED). Help!

require 'net/http'
require 'net/https'
require 'uri'

@http = Net::HTTP.new('https://my.url.com/path', 443)
@http.use_ssl = true
@http.start() { |http|
    req = Net::HTTP.post_form(
        URI.parse('https:///my.url.com/path'),
        {'key1' => 'value1', 'key2' => 'value2'}
    )
    req.basic_auth 'username', 'password'
    response = http.request(req)
    puts response.body      
}

推荐答案

HTTP#post_form将直接执行,而忽略您的其他设置.尝试以下方法:

HTTP#post_form will execute directly, ignoring your other settings. Try this instead:

require 'net/http'
require 'uri'

url = URI.parse('https://my.url.com/path')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'user', 'pass'
req.use_ssl = true
req.form_data({'key1' => 'val1', 'key2' => 'val2'})

resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
puts resp

您可能会遇到服务器证书的麻烦.有关如何获取说明,请参阅我的其他帖子/配置它们.

You are likely to run into trouble with the server's certificates. See my other post for instructions on how to get/configure them.

这篇关于RUBY-SSL,基本身份验证和POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆