Tag: http

使用VAPID进行网络推送:400/401未经授权的注册

首先,我已经检查了这些问题,没有任何运气: Web Push API Chrome,返回“未经授权的注册” [WebPush] [VAPID]请求失败,400 UnauthorizedRegistration 我正在尝试为我正在处理的Web应用程序实现Web推送通知。 目前,我已达到以下目标: 创建VAPID密钥对( 使用本指南 )。 注册服务工作者(只有服务工作者,我认为不再需要manifest.json )。 将用户订阅到服务器(订阅数据将存储在数据库中)。 使用webpush gem发送推送通知。 设置完成后,一切正常(在localhost和远程计算机上)。 但是,在几个小时后(12到24小时之间),通知将停止在远程计算机上工作(localhost工作正常)。 在此之后,从服务器端(Rails)发送推送通知时会引发以下错误: Chrome: UnauthorizedRegistration 400 (无额外信息)。 Firefox: {“code”: 401, “errno”: 109, “error”: “Unauthorized”, “more_info”: “http://autopush.readthedocs.io/en/latest/http.html#error-codes”, “message”: “Request did not validate Invalid bearer token: Auth > 24 hours in the future”} 出现此错误后,我尝试取消订阅并在每次访问页面时重新订阅用户。 重新订阅完成后,db上的订阅字段会更新,但仍会抛出错误,并显示相同的信息。 浏览器和服务工作者都不会抛出任何错误。 我试图通过在Chrome Dev Tools控制台上放置一些js代码,取消注册服务工作者,并重置推送通知权限来手动强制重新订阅,但没有解决错误。 […]

为什么我必须为net :: HTTP请求提供URI.encode甚至安全字符?

我试图使用Net :: HTTP向Twitter发送GET请求(因隐私原因而替换用户ID): url = URI.parse(“http://api.twitter.com/1/friends/ids.json?user_id=12345”) resp = Net::HTTP.get_response(url) 这会在Net :: HTTP中引发exception: NoMethodError:未定义的方法为empty?’ for # from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1470:in empty?’ for # from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1470:in initialize’ 巧合的是,我偶然发现了一个类似的代码片段,它在URI.encode之前使用了URI.parse ,所以我复制了它并再次尝试: url = URI.parse(URI.encode(“http://api.twitter.com/1/friends/ids.json?user_id=12345”)) resp = Net::HTTP.get_response(url) 现在它工作正常,但为什么? 在我提到的URL中没有需要转义的保留字符,为什么我必须调用URI.encode才能成功获取get_response ?

用自定义图像替换破碎的外部图像

我正在遍历在外部站点托管的一系列URL图像字符串。 它看起来像这样: def get_image_urls image_url_array.each do |image_url| puts image_tag image_url end end 这将返回外部站点上托管的图像的URL。 问题是,其中一些图像可能会被破坏(404)。 例如: get_image_urls # These would return image_tags, but for brevity… => “http://sofzh.miximages.com/ruby-on-rails/1.jpg” “http://sofzh.miximages.com/ruby-on-rails/2.jpg” “http://sofzh.miximages.com/ruby-on-rails/3.jpg” # <– (Broken: 404) "http://sofzh.miximages.com/ruby-on-rails/4.jpg" "http://sofzh.miximages.com/ruby-on-rails/5.jpg" # <– (Broken: 404) 我要做的是将损坏图像的URL字符串替换为我自己网站上托管的“缺失”图像。 所以使用上面的例子,3.jpg和5.jpg被打破了,我想要返回这样的东西: get_image_urls # These would return image_tags, but for brevity… => “http://sofzh.miximages.com/ruby-on-rails/1.jpg” “http://sofzh.miximages.com/ruby-on-rails/2.jpg” “http://mysite.com/images/missing.png” “http://sofzh.miximages.com/ruby-on-rails/4.jpg” “http://mysite.com/images/missing.png” […]

如何在ruby Shoes GUI应用程序中创建简单的http请求?

我正在尝试向运行在localhost:3000的本地rails应用程序发出一个简单的http请求。 我的应用程序的代码非常处于原型阶段,因为我只是在尝试其他任何事情之前尝试获取原始function。 rails应用程序在解析之前只返回看起来像这样的JSON “{\”response\”:\”foo is blank\”}” 这是我正在尝试的代码: require ‘net/http’ def get_response return Net::HTTP.get(URI.parse(“http://localhost:3000”)) end Shoes.app(:width => 280, :height => 350) do flow do stack do button “make http request and display results in an alert box” do x = get_response alert x end# of button end# of stack end# of flow end 我也尝试将该方法放在Shoes.app块中无济于事。 执行时,这将创建它应该创建的所有元素,但是当我单击按钮时没有任何反应。 我知道你不应该用ruby鞋做任何实用的事,但我还是在尝试。 […]

Ruby HTTP Library通过Facebook App获得Connection Reset

以下curl命令按预期工作: curl ‘https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=myappid&client_secret=myclientsecret’ 我想在我的Ruby程序中做同样的事情。 以下代码给出了一个错误: fb_access_token_url = URI.parse( ‘https://graph.facebook.com/oauth/access_token’ + ‘?grant_type=client_credentials’ + ‘&client_id=’ + FACEBOOK_APP_ID + ‘&client_secret=’ + FACEBOOK_APP_SECRET) fb_access_token = Net::HTTP.get(fb_access_token_url) 这段代码也是如此: fb_access_token_host = ‘graph.facebook.com’ fb_access_token_path_and_params = ( ‘/oauth/access_token’ + ‘?grant_type=client_credentials’ + ‘&client_id=’ + FACEBOOK_APP_ID + ‘&client_secret=’ + FACEBOOK_APP_SECRET) https_port = Net::HTTP.https_default_port() fb_access_token = Net::HTTP.get_response(fb_access_token_host, fb_access_token_path_and_params, https_port) 错误如下: Errno::ECONNRESET: Connection reset by peer /usr/lib/ruby/1.8/net/protocol.rb:135:in […]

Webrick透明代理

我有一个绝对简单的代理运行。 require ‘webrick’ require ‘webrick/httpproxy’ s = WEBrick::HTTPProxyServer.new(:Port => 8080, :RequestCallback => Proc.new{|req,res| puts req.request_line, req.raw_header}) # Shutdown functionality trap(“INT”){s.shutdown} # run the beast s.start 在我看来,这应该不会以任何方式影响沟通。 但是有些网站不再有用了。 特别是http://lastfm.de的嵌入式闪存播放器不起作用。 标题看起来链接: – -> http://ext.last.fm/2.0/?api%5Fsig=aa3e9ac9edf46ceb9a673cb76e61fef4&flashresponse=true&y=1269686332&streaming=true&playlistURL=lastfm%3A%2F%2Fplaylist%2Ftrack%2F42620245&fod=true&sk=ee93ae4f438767bf0183d26478610732&lang=de&api%5Fkey=da6ae1e99462ee22e81ac91ed39b43a4&method=playlist%2Efetch GET http://play.last.fm/preview/118270350.mp3 HTTP/1.1 Host: play.last.fm User-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.2) Gecko/20100308 Ubuntu/10.04 (lucid) Firefox/3.6 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: de,en-us;q=0.7,en;q=0.3 Accept-Encoding: gzip,deflate […]

在下载和计算已经下载的数量之前获取文件大小(http + ruby​​)

任何人都可以帮助我 在开始下载之前获取文件大小 显示已下载了多少% 。 require ‘net/http’ require ‘uri’ url = “http://www.onalllevels.com/2009-12-02TheYangShow_Squidoo_Part 1.flv” url_base = url.split(‘/’)[2] url_path = ‘/’+url.split(‘/’)[3..-1].join(‘/’) Net::HTTP.start(url_base) do |http| resp = http.get(URI.escape(url_path)) open(“test.file”, “wb”) do |file| file.write(resp.body) end end puts “Done.”

在Ruby中获取响应大小的优化方法?

我可以使用File.size(path)来获取文件的大小(以字节为单位)。 如何在不将其写入临时文件的情况下获取HTTP响应的大小?

Ruby HTTP得到params

如何通过ruby发送带参数的HTTP GET请求? 我尝试了很多例子,但所有这些都失败了。

Rails 3,HTTP扩展(WebDAV)和Rack App安装

1以下更多内容是指代代码开发了一个可以被认为是缺陷的rails的问题。 2我也问过一些了解得更好的人的一些观点。 我想通过Warden身份validation将WebDAV添加到我的Rails 3应用程序中。 我的warden中间件是通过Devise注入的。 http://github.com/chrisroberts/dav4rack http://github.com/hassox/warden http://github.com/plataformatec/devise 我无法从rails app(routes)中挂载DAV4Rack处理程序,如下所示: # in config/routes.rb mount DAV4Rack::Handler.new( :root => Rails.root.to_s, # ‘/webdav’, :resource_class => Dav::DocumentResource # “/webdav” 因为railsvalidationHTTP谓词(GET POST PUT ..),而webdav使用像PROPFIND这样的HTTP扩展不能validation,抛出以下exception: ActionController::UnknownHttpMethod (PROPFIND, accepted HTTP methods are get, head, put, post, delete, and options) 此validation发生在ActionDispatch中: /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/http/request.rb +56 +72 in (56) “def request_method” and (72) “def method” 来自ActionDispatch的示例代码,用于执行validation,以明确说明: […]