2011-01-12 115 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被用来逃跑格式化字符串。

希望这会有所帮助。