2011-06-24 51 views
0

我有這個代碼的問題的問題'T喜歡:RDF ::讀卡器>與URI :: InvalidError

http://sparql.reegle.info?query=CONSTRUCT+{+%3Chttp://reegle.info/countries/IN%3E+?p+?o.+%3Chttp://reegle.info/countries/IN.rdf%3E+foaf:primaryTopic+%3Chttp://reegle.info/countries/IN%3E;+cc:license+%3Chttp://www.nationalarchives.gov.uk/doc/open-government-licence%3E;+cc:attributionName+"REEEP";+cc:attributionURL+%3Chttp://reegle.info/countries/IN%3E.+}+WHERE+{+%3Chttp://reegle.info/countries/IN%3E+?p+?o.}&format=application/rdf%2Bxml 

所以我得到以下錯誤:

URI::InvalidURIError: bad URI(is not URI?) 

任何想法,如何解決這個問題?

謝謝

P.S.做類似URI.parse(URI.encode([url])))在這裏沒有任何效果。

回答

1

URI不喜歡該URL中的雙引號或大括號。您可以通過手這樣的修復URI:

# This auto-populating cache isn't necessary but... 
replacements = Hash.new { |h,k| h[k] = URI.encode(k) } 
broken_uri.gsub!(/[{}"]/) { replacements[$&] } 

RFC 1738: Uniform Resource Locators (URL)

Thus, only alphanumerics, the special characters " $-_.+!*'(), ", and reserved characters used for their reserved purposes may be used unencoded within a URL.

所以我想說的是reegle.info應該是URL編碼更多的東西比他們。 OTOH,Ruby的URI類可能會更寬容一些(例如,Perl的URI class將接受該URI作爲輸入,但它將雙引號和大括號轉換爲輸出上的百分比編碼形式)。