0

我应该在哪里对我的 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)

哪个是正确的,哪个更好?

4

1 回答 1

1

我通常喜欢使用URIencode_www_form,它采用数组或名称/值对的散列。这是文档中的示例:

需要'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"
于 2013-09-17T17:02:32.560 回答