我应该在哪里对我的POST请求数据进行URL编码?对于每个参数,还是整个查询行?
例如,我有这段代码:
@VIEWSTATE = url_encode(@VIEWSTATE)
data = ("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=#{@VIEWSTATE}&__EVENTVALIDATION=#{@EVENTVALIDATION}&=ctl0.......
headers = {
'Cookie' => cookie,
***
}
resp, data = http.post(path, data, headers)
是对的,还是我必须写的:
@VIEWSTATE = @VIEWSTATE
data = url_encode("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=#{@VIEWSTATE}&__EVENTVALIDATION=#{@EVENTVALIDATION}&=ctl0.......
headers = {
'Cookie' => cookie,
***
}
resp, data = http.post(path, data, headers)
哪个是对的,哪个更好?
答案 0 :(得分:1)
我通常喜欢使用URI's encode_www_form
来获取数组或名称/值对的散列。这是the documentation的示例:
require 'uri' URI.encode_www_form([["q", "ruby"], ["lang", "en"]]) #=> "q=ruby&lang=en" URI.encode_www_form("q" => "ruby", "lang" => "en") #=> "q=ruby&lang=en" URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en") #=> "q=ruby&q=perl&lang=en" URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]]) #=> "q=ruby&q=perl&lang=en"