save response cookies
parent
52b77131dc
commit
470fdacee4
2
main.cr
2
main.cr
|
@ -7,4 +7,4 @@ page = agent.get("http://example.com/", params: params)
|
||||||
#puts page.code
|
#puts page.code
|
||||||
#puts page.body
|
#puts page.body
|
||||||
#puts page.css("h1").first.inner_text
|
#puts page.css("h1").first.inner_text
|
||||||
puts page.title
|
#puts page.title
|
||||||
|
|
|
@ -13,11 +13,14 @@ module MechanizeCr
|
||||||
|
|
||||||
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new)
|
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new)
|
||||||
uri = URI.parse(uri)
|
uri = URI.parse(uri)
|
||||||
add_request_headers(headers)
|
set_request_headers(headers)
|
||||||
params = hash_to_params(params)
|
params = hash_to_params(params)
|
||||||
response = http_request uri, method, params
|
response = http_request uri, method, params
|
||||||
body = response.not_nil!.body
|
body = response.not_nil!.body
|
||||||
page = response_parse(response, body, uri)
|
page = response_parse(response, body, uri)
|
||||||
|
# save cookies
|
||||||
|
add_response_cookies(response, uri, page)
|
||||||
|
page
|
||||||
end
|
end
|
||||||
|
|
||||||
def http_request(uri, method, params)
|
def http_request(uri, method, params)
|
||||||
|
@ -32,7 +35,7 @@ module MechanizeCr
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def add_request_headers(headers)
|
private def set_request_headers(headers)
|
||||||
headers.each do |k,v|
|
headers.each do |k,v|
|
||||||
request_headers[k] = v
|
request_headers[k] = v
|
||||||
end
|
end
|
||||||
|
@ -51,6 +54,20 @@ module MechanizeCr
|
||||||
private def response_parse(response, body, uri)
|
private def response_parse(response, body, uri)
|
||||||
@context.not_nil!.parse uri, response, body
|
@context.not_nil!.parse uri, response, body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def add_response_cookies(response, uri, page)
|
||||||
|
#if page.body =~ /Set-Cookie/
|
||||||
|
# page.css("//head/meta[@http-equiv=\"Set-Cookie\"]").each do |meta|
|
||||||
|
# save_cookies(uri, meta["content"])
|
||||||
|
# end
|
||||||
|
#end
|
||||||
|
header_cookies = response.try &.cookies
|
||||||
|
if header_cookies.try &.empty?
|
||||||
|
request_headers
|
||||||
|
else
|
||||||
|
header_cookies.not_nil!.add_request_headers(request_headers)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue