ruby – 我做错了还是nethttp中的错误?

ruby – 我做错了还是nethttp中的错误?,第1张

概述我在NginX / Phusion Passenger服务器下使用 Ruby Net :: HTTP,试图将JSON字符串发送到服务器.看来我的POST只在作为’application / json’发送时,过早地关闭了到服务器的会话.即: 在服务器端: 127.0.0.1 - - [03/Sep/2013 07:47:14] "POST /path/to/submit " 200 45 0.00 我在Nginx / Phusion Passenger服务器下使用 Ruby Net :: http,试图将JsON字符串发送到服务器.看来我的POST只在作为’application / Json’发送时,过早地关闭了到服务器的会话.即:

在服务器端:

127.0.0.1 - - [03/Sep/2013 07:47:14] "POST /path/to/submit " 200 45 0.0013pID=12893 thr=47197563169088 file=ext/Nginx/HelperAgent.cpp:933 time=2013-09-03 07:47:14.830 ]: Uncaught exception in PassengerServer clIEnt thread:exception: Cannot read response from backend process: Connection reset by peer (104)backtrace: in 'voID ClIEnt::forwardResponse(Passenger::SessionPtr&,Passenger::fileDescriptor&,const Passenger::AnalyticslogPtr&)' (HelperAgent.cpp:698) in 'voID ClIEnt::handleRequest(Passenger::fileDescriptor&)' (HelperAgent.cpp:859) in 'voID ClIEnt::threadMain()' (HelperAgent.cpp:952)

客户端调试会话是:

opening connection to hostname.com...opened<- "POST /path/to/submit http/1.1\r\nContent-Type: application/Json\r\nAccept: application/Json\r\nUser-Agent: agent+test\r\nConnection: close\r\nHost: hostname.com\r\nContent-Length: 660\r\n\r\n"<- "[{Json string}]"-> "http/1.1 301 Moved Permanently\r\n"-> "Server: Nginx/1.2.6\r\n"-> "Date: Tue,03 Sep 2013 14:47:15 GMT\r\n"-> "Content-Type: text/HTML\r\n"-> "Content-Length: 184\r\n"-> "Connection: close\r\n"-> "Location: https://hostname.com/path/to/submit\r\n"-> "\r\n"reading 184 bytes...-> "<HTML>\r\n<head><Title>301 Moved Permanently</Title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>301 Moved Permanently</h1></center>\r\n<hr><center>Nginx/1.2.6</center>\r\n</body>\r\n</HTML>\r\n"read 184 bytesConn closeopening connection to hostname.com...opened<- "POST /path/to/submit http/1.1\r\nContent-Type: application/Json\r\nAccept: application/Json\r\nUser-Agent: agent+test\r\nConnection: close\r\nHost: hostname.com\r\nContent-Length: 660\r\n\r\n"<- "[{Json string}]"-> "http/1.1 200 OK\r\n"-> "Content-Type: text/HTML;charset=utf-8\r\n"-> "Content-Length: 45\r\n"-> "Connection: close\r\n"-> "Status: 200\r\n"-> "X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.17\r\n"-> "x-frame-options: SAMEORIGIN\r\n"-> "X-XSS-Protection: 1; mode=block\r\n"-> "X-Content-Type-Options: nosniff\r\n"-> "Server: Nginx/1.2.6 + Phusion Passenger 3.0.17 (mod_rails/mod_rack)\r\n"-> "Strict-Transport-Security: max-age=31536000\r\n"-> "x-frame-options: DENY\r\n"-> "\r\n"reading 45 bytes...-> "some url string"  <-- this is suspicous to me.read 45 bytesConn close

我用来提交数据的代码是:

privatedef _http_clIEnt(method = 'get',location = nil,limit = 10)     raise ArgumentError,'FATAL: too many http redirects attempted...' if limit == 0    response = nil     if location.nil?        if @uri.nil?            raise ArgumentError 'FATAL: no location specifIEd. Quitting.'        end     else        @uri = URI.parse(location)    end     clIEnt = Net::http.new(@uri.host,@uri.port)    clIEnt.open_timeout    = 15 # in seconds    clIEnt.read_timeout    = 15 # in seconds    initheader             = {'content-type' => 'application/Json','accept'       => 'application/Json','user-agent'   => UA,}    clIEnt.set_deBUG_output $stderr  # XXX TEMPORARY    if @uri.scheme == 'https'        clIEnt.use_ssl = true        clIEnt.verify_mode = 0     end     if method == 'post'        serialized = JsON.generate(@payload)        response   = clIEnt.send_request('POST',@uri.path,serialized,initheader = initheader)        puts serialized    end # Only intended for Pings. In order to do# full GETs (including query_params) this# will need to be refactored a little.    if method == 'get'        response = clIEnt.get(@uri.path)    end     case response        when Net::httpSuccess            @status = true        when Net::httpMovedPermanently            @status = false            limit = limit - 1             _http_clIEnt(method,response['location'],limit)        when Net::httpClIEntError            $stderr.puts 'ClIEnt error.'            @status = false        when Net::httpServerError            $stderr.puts 'Server error.'            @status = false        else            $stderr.puts 'No IDea what the server returned.'            @status = false    end     unless response.nil?        @response  = {:code => response.code,:body => (response.body || 'OK')}        @code      = @response[:code].to_s        @http_body = @response[:body].to_s    else        $stderr.puts 'response from server was empty. :('    end     _send_statusend

我正在使用:

> Ruby版本:1.9.3p448
> Nginx 1.2.6
>乘客3.0.17

我很确定我可能做错了什么,但我是Ruby的新手,也是这种网络编程的新手.

上面的代码片段使用send_request方法,但我也尝试过post和post2,结果相同.

我真的很怀疑,只要在我的initheader中使用’application / Json’,一切都会中断.当我删除它,一切正常.

解决方法 尝试升级到Phusion Passenger 4.它可以更好地处理I / O异步,并且在许多情况下可以避免过早的连接关闭问题. 总结

以上是内存溢出为你收集整理的ruby – 我做错了还是net / http中的错误?全部内容,希望文章能够帮你解决ruby – 我做错了还是net / http中的错误?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1269159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存