SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

时间:2021-11-24 00:28:47

在文章 Ruby On Rails中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求访问IBM Bluemix上的语言翻译服务时,提示:SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

例如下面的代码:

		auth = "c9819718-4660-441c-9df7-07398950ea44:qUvrJPSadgOx";
		surl = "https://" + auth + "@gateway.watsonplatform.net/language-translation/api/v2/translate?source=en&target=es&text=" + @txt;
		uri = URI.parse(surl);		
			begin #开始
			http = Net::HTTP.new(uri.host, uri.port)
			http.use_ssl = true
			
			request = Net::HTTP::Get.new(uri.request_uri)
			
			response = http.request(request)
			@result = "{\"text\":\"#{response.body}\"}";
		rescue
			@result = "{\"error\":\"#{$!}!\"}";
		end
我们注意到,URL是HTTPS开头,需要设置:http.use_ssl = true

但只是设置了http.use_ssl = true,但不设置http.verify_mode的话,就会出现此错误;


设置方法:

如果你访问的地址不需要证书,设置如下:

http.verify_mode = OpenSSL::SSL::VERIFY_NONE


如果使用了证书,设置如下:

http.verify_mode = OpenSSL::SSL::VERIFY_PEER

https.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt'

按我的理解应该是这样设置,具体还没 有测试过。