[英]Ruby: Can net/http make a GET and POST request simultaneously?
Is it possible to pass both the GET and POST parameters at the same time? 是否可以同时传递GET和POST参数?
uri = URI.parse("http://www.example.com/post.php?a=1&b=2")
req = Net::HTTP::Post.new(uri.path, {
'Referer' => "http://www.example.com/referer",
'User-Agent'=> "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
'Cookie' => $cookie
})
req.set_form_data({
'foo' => 'bar',
'bar' => 'foo'
})
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 40
http.read_timeout = 20
# Request page:
begin
resp = http.request(req)
rescue Exception
puts "Exception requesting the page; returning"
end
In the script above, only the POST parameters get sent and the GET query is ignored 在上面的脚本中,只发送POST参数并忽略GET查询
When creating the request you just need to make sure to keep the GET params in the path: 创建请求时,您只需确保将GET参数保留在路径中:
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}", {
'Referer' => "http://www.example.com/referer",
'User-Agent'=> "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
'Cookie' => $cookie
})
Notice that instead of just uri.path
, I append the ?
请注意,我不仅仅是
uri.path
,而是追加?
and uri.query
to it. 并且
uri.query
到它。 This should pass the GET parameters as well as the POST ones. 这应该传递GET参数以及POST参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.