2011-01-12 114 views
0

我試圖訪問Dreamhost API來發送郵件。創建複雜的網址

的API預計含有域的地址與實際emailcontent

domain = 'https://api.dreamhost.com/' 
key = "dq86ds5qd4sq" 
command = "announcement_list-post_announcement" 
mailing = "mailing" 
domain = "domain.nl" 
listname = "mailing Adventure"<[email protected]>" 
message = '<html>html here</html>' 

url = domain + "&key=#{key}&cmd=#{command}&listname=#{mailing}&domain=#{domain}&listname=#{listname}&message=#{message}" 

uri = URI.parse(url) 
http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

http.start { 
# http.request_get(uri.path) {|res| 
# print res.body 
# } 
} 

當我解析URL,我得到一個錯誤

壞URI(是不是URI?)

url包含來自listname和message的url本身我認爲這會導致問題。我不知道如何去讚美這件事。 CGI escpae已被提出,但似乎將空白轉換爲+。

有人知道如何解決這個問題嗎?

感謝

回答

2

此行

url = domain + "&key=#{key}&cmd=#{command}&listname=#{mailing}&domain=#{domain}&listname=#{listname}&message=#{message}" 

的URL後現身爲:

"domain.nl&key=dq86ds5qd4sq&cmd=announcement_list-post_announcement&listname=mailing&domain=domain.nl&listname=mailing Adventure<[email protected]>&message=<html>html here</html>" 

這是不正確的URL。這就是生成異常的原因。

一個速戰速決如下:

url = "http://" + domain + "/?key=#{key}&cmd=#{command}&listname=#{mailing}&domain=#{domain}&listname=#{URI.escape(listname)}&message=#{URI.escape(message)}" 

uri = URI.parse(url) 

URI.escape被用來逃跑格式化字符串。

希望這會有所幫助。