Ruby:net/http 可以同时发出 GET 和 POST 请求吗?
是否可以同时传递 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
在上面的脚本中,仅发送 POST 参数并忽略 GET 查询
Is it possible to pass both the GET and POST parameters at the same time?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建请求时,您只需确保将 GET 参数保留在路径中:
请注意,我附加了
?
和,而不只是
到它。这应该传递 GET 参数以及 POST 参数。uri.path
uri.queryWhen creating the request you just need to make sure to keep the GET params in the path:
Notice that instead of just
uri.path
, I append the?
anduri.query
to it. This should pass the GET parameters as well as the POST ones.