首页 > 代码库 > ruby on rails模拟HTTP请求错误发生:end of file reached

ruby on rails模拟HTTP请求错误发生:end of file reached

在文章 Ruby On Rails中REST API使用演示样例——基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求訪问IBM Bluemix上的

source=en&target=es&text=hello" style="color: rgb(51, 102, 153); text-decoration: none; font-family: Arial; font-size: 14px; line-height: 26px;">语言翻译服务时,提示:end of file reached

代码例如以下:

auth = "c9819718-4660-441c-9df7-07398950ea44:qUvrJPqwsgOx";
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

设置之后,又出来新的问题:SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

ruby on rails模拟HTTP请求错误发生:end of file reached