Erlang获取最终重定向的URL

时间:2017-02-21 12:36:46

标签: get erlang url-redirection httpc

正如this问题中所述,我使用以下方法查找最终重定向的网址

URL="http://mail.google.com",
HTTPOpts = [{autoredirect, false}],
perform_request(URL) ->
case httpc:request(get, {URL, [{"User-Agent", "Mozilla"}]}, HTTPOpts, []) of
   {ok, {{_, Code, _}, Headers, Body}}  when Code == 200  ->

       %%code_to_process_the_URL%%

   {ok, {{_, Code, _}, Headers, _}}  when Code < 310 , Code >= 300 ->

     NewURL=proplists:get_value("location", Headers),
     perform_request(NewURL)
     end

这适用于其他网址,但它面临URL = https://mail.google.com的问题,因为它的第一个位置标头是location = /mail/,这不是有效的网址,而且我得到一个空白网页。

我还使用GET命令生成输出以在终端上验证这一点。

输出:

mandeep@mandeep-Inspiron-5447:~$ GET -S -d -e http://mail.google.com
GET http://mail.google.com
301 Moved Permanently
Cache-Control: private, max-age=0
Connection: close
Date: Tue, 21 Feb 2017 11:56:14 GMT
Accept-Ranges: none
Location: /mail/
Server: GSE
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Expires: Tue, 21 Feb 2017 11:56:14 GMT
Client-Date: Tue, 21 Feb 2017 11:55:30 GMT
Client-Peer: 216.58.197.69:80
Client-Response-Num: 1
Title: Moved Permanently
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block

GET http://mail.google.com/mail/
302 Moved Temporarily
Cache-Control: private, max-age=0
Connection: close
Date: Tue, 21 Feb 2017 11:56:15 GMT
Accept-Ranges: none
Location: https://mail.google.com/mail/
Server: GSE
Vary: Accept-Encoding 
Content-Type: text/html; charset=UTF-8
Expires: Tue, 21 Feb 2017 11:56:15 GMT
Client-Date: Tue, 21 Feb 2017 11:55:30 GMT
Client-Peer: 172.217.26.165:80
Client-Response-Num: 1
Title: Moved Temporarily
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block

GET https://mail.google.com/mail/
302 Moved Temporarily
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: close
Date: Tue, 21 Feb 2017 11:56:21 GMT
Pragma: no-cache
Accept-Ranges: none
Location: https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1
Server: GSE
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Alt-Svc: quic=":443"; ma=2592000; v="35,34"
Client-Date: Tue, 21 Feb 2017 11:55:36 GMT
Client-Peer: 216.58.197.69:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/O=Google Inc/CN=Google Internet
Authority G2
Client-SSL-Cert-Subject: /C=US/ST=California/L=Mountain View/O=Google        Inc/CN=mail.google.com
Client-SSL-Cipher: ECDHE-ECDSA-AES128-GCM-SHA256
Client-SSL-Socket-Class: IO::Socket::SSL
Title: Moved Temporarily
X-Content-Type-Options: nosniff 
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block

GET https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1
200 OK
Cache-Control: no-cache, no-store
Connection: close
Date: Tue, 21 Feb 2017 11:56:22 GMT
Pragma: no-cache
Server: GSE
Content-Type: text/html; charset=UTF-8
Expires: Mon, 01-Jan-1990 00:00:00 GMT
Alt-Svc: quic=":443"; ma=2592000; v="35,34"
Client-Date: Tue, 21 Feb 2017 11:55:38 GMT
Client-Peer: 172.217.26.173:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/O=Google Inc/CN=Google Internet     Authority G2
Client-SSL-Cert-Subject: /C=US/ST=California/L=Mountain View/O=Google    Inc/CN=accounts.google.com
Client-SSL-Cipher: ECDHE-RSA-AES128-SHA
Client-SSL-Socket-Class: IO::Socket::SSL
Client-Transfer-Encoding: chunked
Link: <https://www.google.com/gmail/>; rel="canonical"
Set-Cookie:   GAPS=1:uiUyF1S0WckgUUlRhZmrUeuRVCgCiA:9vKBlzT8ecd7l7Ob;Path=/;Expires=Thu,    21-Feb-2019 11:56:22 GMT;Secure;HttpOnly;Priority=HIGH
Set-Cookie: GALX=mfArYRFLcco;Path=/;Secure
Strict-Transport-Security: max-age=10893354; includeSubDomains
Title: Gmail
X-Auto-Login:realm=com.google&args=service%3Dmail%26continue%3Dhttps%253A%252F%252Fmail.google.com%252Fmail%252F
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Meta-Charset: utf-8
X-Meta-Description: Gmail is email that's intuitive, efficient, and
useful. 15 GB of storage, less spam, and mobile access.
X-Meta-Google-Site-Verification: LrdTUW9psUAMbh4Ia074-BPEVmcpBxF6Gwf0MSgQXZs
X-Meta-Viewport: width=300, initial-scale=1
X-XSS-Protection: 1; mode=block

如何解决此问题?

0 个答案:

没有答案