我的简单代码旨在登录到Web应用程序(loginUrl - 使用http POST),然后转到同一个应用程序中的另一个页面(theurl - 使用GET)。
这是我的代码:
host = 'http://myhost.mydomain:9080'
theurl = host + '/am/ve/mod/console/'#?orderBy=ORDER_BY_ADMIN_APP&order=ASC&viewBy=1&pagesize=20'
username = 'myuser'
password = 'mypass'
loginUrl = host + '/am/en/common/j_security_check'
values = {}
values['submit'] = '1'
values['dest'] = 'null'
values['j_username'] = username
values['j_password'] = password
data = urllib.urlencode(values)
headers = {'Cache-control': 'max-age=0', 'Pragma': 'no-cache', 'User-agent': 'Mozilla/5.0'}
cookieprocessor = urllib2.HTTPCookieProcessor()
http_handler = urllib2.HTTPHandler(debuglevel=1)
opener = urllib2.build_opener(http_handler, cookieprocessor)
print "========================= POST request ====================="
request = urllib2.Request(loginUrl, data, headers)
opener.open(request)
print "========================= GET request ====================="
request = urllib2.Request(theurl, headers=headers, unverifiable=True)
page = opener.open(request)
print "Final url: " + page.geturl()
输出结果为:
========================= POST request =====================
send: 'POST /am/en/common/j_security_check HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-Length: 55\r\nConnection: close\r\nUser-Agent: Mozilla/5.0\r\nHost: myhost.mydomain:9080\r\nPragma: no-cache\r\nCache-Control: max-age=0\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: 'dest=null&j_username=myuser&j_password=mypass&submit=1'
reply: 'HTTP/1.1 302 Found\r\n'
header: Location: http://myhost.mydomain:9080/am/en/common/
header: Content-Language: en-US
header: Content-Length: 0
header: Set-Cookie: JSESSIONID=0000AioGqwi8QbBFL0Yt1h3wLq4:-1; Path=/
header: Set-Cookie: LtpaToken2=AqDVFChifqCAOm3ZYllczyUV/3hWa8tcnQx6E1/E9bA9vkT+XMflFJjhm1X2WT6ZNrix5FUO34X8dLjxYMcCujlH16Gp0NevYHu3LUtDVdopOiLDIxbqrNX9aK1hhSQaivq1uyaboYJsivmUbvKHEFftg8IDieSv0Img5ytfkEFtSL8XxRlg0sSlEADlzltydg+m5cOeOaEcJWBkYeRdOZeK+KWp/3mBInWknReiUjuUKAmvawBqomGjqfEvJdLeSWfBcPyuiVleDoX5Ww8nLstntFK/17KrGMSzVaX+KDF9hMCkQWq6yZIU76nousJMXw5mAKXlWQI5K74QYTqM/mI6uxLq57CCZdBtHDaHRvFIiMWP2kWxQab0JOLwamYTWWoDo7Dq4NTUH2zAjUn/6Rb08Bwq5yB6LG3TYZFsItV8no7cZ59tmgCz/C8HyWVpilnuv8fF9UU++z9e9awwoo1gzdhRy3ZeippAmUGrKHiu3EN6vREE27oDVRNeIjIMfE26LDsuDLiZt+u2B12xR+kWKxCiPzSi00olR/2tfwoIBU6LU7fvs57OiNhOW9SrjinYeNrawbVBdcQQhsGRZhvrBmeW3N0oOjqVRerWAsQrTEneqrJ4P/gV6zLN4V6V; Path=/
header: Connection: Close
header: Date: Tue, 03 Apr 2012 14:00:25 GMT
header: Server: WebSphere Application Server/7.0
header: Expires: Thu, 01 Dec 1994 16:00:00 GMT
header: Cache-Control: no-cache="set-cookie, set-cookie2"
send: 'GET /am/en/common/ HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: myhost.mydomain:9080\r\nUser-Agent: Mozilla/5.0\r\nConnection: close\r\nCookie: LtpaToken2=AqDVFChifqCAOm3ZYllczyUV/3hWa8tcnQx6E1/E9bA9vkT+XMflFJjhm1X2WT6ZNrix5FUO34X8dLjxYMcCujlH16Gp0NevYHu3LUtDVdopOiLDIxbqrNX9aK1hhSQaivq1uyaboYJsivmUbvKHEFftg8IDieSv0Img5ytfkEFtSL8XxRlg0sSlEADlzltydg+m5cOeOaEcJWBkYeRdOZeK+KWp/3mBInWknReiUjuUKAmvawBqomGjqfEvJdLeSWfBcPyuiVleDoX5Ww8nLstntFK/17KrGMSzVaX+KDF9hMCkQWq6yZIU76nousJMXw5mAKXlWQI5K74QYTqM/mI6uxLq57CCZdBtHDaHRvFIiMWP2kWxQab0JOLwamYTWWoDo7Dq4NTUH2zAjUn/6Rb08Bwq5yB6LG3TYZFsItV8no7cZ59tmgCz/C8HyWVpilnuv8fF9UU++z9e9awwoo1gzdhRy3ZeippAmUGrKHiu3EN6vREE27oDVRNeIjIMfE26LDsuDLiZt+u2B12xR+kWKxCiPzSi00olR/2tfwoIBU6LU7fvs57OiNhOW9SrjinYeNrawbVBdcQQhsGRZhvrBmeW3N0oOjqVRerWAsQrTEneqrJ4P/gV6zLN4V6V; JSESSIONID=0000AioGqwi8QbBFL0Yt1h3wLq4:-1\r\nPragma: no-cache\r\nCache-Control: max-age=0\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Type: text/html;charset=ISO-8859-1
header: Content-Language: en-US
header: Content-Length: 1323
header: Connection: Close
header: Date: Tue, 03 Apr 2012 14:00:26 GMT
header: Server: WebSphere Application Server/7.0
========================= GET request =====================
send: 'GET /am/ve/mod/console/ HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: myhost.mydomain:9080\r\nUser-Agent: Mozilla/5.0\r\nConnection: close\r\nCookie: LtpaToken2=AqDVFChifqCAOm3ZYllczyUV/3hWa8tcnQx6E1/E9bA9vkT+XMflFJjhm1X2WT6ZNrix5FUO34X8dLjxYMcCujlH16Gp0NevYHu3LUtDVdopOiLDIxbqrNX9aK1hhSQaivq1uyaboYJsivmUbvKHEFftg8IDieSv0Img5ytfkEFtSL8XxRlg0sSlEADlzltydg+m5cOeOaEcJWBkYeRdOZeK+KWp/3mBInWknReiUjuUKAmvawBqomGjqfEvJdLeSWfBcPyuiVleDoX5Ww8nLstntFK/17KrGMSzVaX+KDF9hMCkQWq6yZIU76nousJMXw5mAKXlWQI5K74QYTqM/mI6uxLq57CCZdBtHDaHRvFIiMWP2kWxQab0JOLwamYTWWoDo7Dq4NTUH2zAjUn/6Rb08Bwq5yB6LG3TYZFsItV8no7cZ59tmgCz/C8HyWVpilnuv8fF9UU++z9e9awwoo1gzdhRy3ZeippAmUGrKHiu3EN6vREE27oDVRNeIjIMfE26LDsuDLiZt+u2B12xR+kWKxCiPzSi00olR/2tfwoIBU6LU7fvs57OiNhOW9SrjinYeNrawbVBdcQQhsGRZhvrBmeW3N0oOjqVRerWAsQrTEneqrJ4P/gV6zLN4V6V; JSESSIONID=0000AioGqwi8QbBFL0Yt1h3wLq4:-1\r\nPragma: no-cache\r\nCache-Control: max-age=0\r\n\r\n'
reply: 'HTTP/1.1 302 Found\r\n'
header: Cache-Control: no-cache
header: Pragma: no-cache
header: Expires: -1
header: Content-Type: text/html; charset=ISO-8859-1
header: Location: http://myhost.mydomain:9080/am/ve/login?dest=http%3A%2F%2Fmyhost.mydomain%3A9080%2Fam%2Fve%2Fmod%2Fconsole%2F&rand=1333461626691
header: Content-Language: en
header: Content-Length: 0
header: Connection: Close
header: Date: Tue, 03 Apr 2012 14:00:26 GMT
header: Server: WebSphere Application Server/7.0
send: 'GET /am/ve/login?dest=http%3A%2F%2Fmyhost.mydomain%3A9080%2Fam%2Fve%2Fmod%2Fconsole%2F&rand=1333461626691 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: myhost.mydomain:9080\r\nUser-Agent: Mozilla/5.0\r\nConnection: close\r\nCookie: LtpaToken2=AqDVFChifqCAOm3ZYllczyUV/3hWa8tcnQx6E1/E9bA9vkT+XMflFJjhm1X2WT6ZNrix5FUO34X8dLjxYMcCujlH16Gp0NevYHu3LUtDVdopOiLDIxbqrNX9aK1hhSQaivq1uyaboYJsivmUbvKHEFftg8IDieSv0Img5ytfkEFtSL8XxRlg0sSlEADlzltydg+m5cOeOaEcJWBkYeRdOZeK+KWp/3mBInWknReiUjuUKAmvawBqomGjqfEvJdLeSWfBcPyuiVleDoX5Ww8nLstntFK/17KrGMSzVaX+KDF9hMCkQWq6yZIU76nousJMXw5mAKXlWQI5K74QYTqM/mI6uxLq57CCZdBtHDaHRvFIiMWP2kWxQab0JOLwamYTWWoDo7Dq4NTUH2zAjUn/6Rb08Bwq5yB6LG3TYZFsItV8no7cZ59tmgCz/C8HyWVpilnuv8fF9UU++z9e9awwoo1gzdhRy3ZeippAmUGrKHiu3EN6vREE27oDVRNeIjIMfE26LDsuDLiZt+u2B12xR+kWKxCiPzSi00olR/2tfwoIBU6LU7fvs57OiNhOW9SrjinYeNrawbVBdcQQhsGRZhvrBmeW3N0oOjqVRerWAsQrTEneqrJ4P/gV6zLN4V6V; JSESSIONID=0000AioGqwi8QbBFL0Yt1h3wLq4:-1\r\nPragma: no-cache\r\nCache-Control: max-age=0\r\n\r\n'
reply: 'HTTP/1.1 302 Found\r\n'
header: Cache-Control: no-cache
header: Pragma: no-cache
header: Expires: -1
header: Content-Type: text/html; charset=ISO-8859-1
header: Location: http://myhost.mydomain:9080/am/home?rand=1333461626997
header: Content-Language: en
header: Content-Length: 0
header: Connection: Close
header: Date: Tue, 03 Apr 2012 14:00:26 GMT
header: Server: WebSphere Application Server/7.0
send: 'GET /am/home?rand=1333461626997 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: myhost.mydomain:9080\r\nUser-Agent: Mozilla/5.0\r\nConnection: close\r\nCookie: LtpaToken2=AqDVFChifqCAOm3ZYllczyUV/3hWa8tcnQx6E1/E9bA9vkT+XMflFJjhm1X2WT6ZNrix5FUO34X8dLjxYMcCujlH16Gp0NevYHu3LUtDVdopOiLDIxbqrNX9aK1hhSQaivq1uyaboYJsivmUbvKHEFftg8IDieSv0Img5ytfkEFtSL8XxRlg0sSlEADlzltydg+m5cOeOaEcJWBkYeRdOZeK+KWp/3mBInWknReiUjuUKAmvawBqomGjqfEvJdLeSWfBcPyuiVleDoX5Ww8nLstntFK/17KrGMSzVaX+KDF9hMCkQWq6yZIU76nousJMXw5mAKXlWQI5K74QYTqM/mI6uxLq57CCZdBtHDaHRvFIiMWP2kWxQab0JOLwamYTWWoDo7Dq4NTUH2zAjUn/6Rb08Bwq5yB6LG3TYZFsItV8no7cZ59tmgCz/C8HyWVpilnuv8fF9UU++z9e9awwoo1gzdhRy3ZeippAmUGrKHiu3EN6vREE27oDVRNeIjIMfE26LDsuDLiZt+u2B12xR+kWKxCiPzSi00olR/2tfwoIBU6LU7fvs57OiNhOW9SrjinYeNrawbVBdcQQhsGRZhvrBmeW3N0oOjqVRerWAsQrTEneqrJ4P/gV6zLN4V6V; JSESSIONID=0000AioGqwi8QbBFL0Yt1h3wLq4:-1\r\nPragma: no-cache\r\nCache-Control: max-age=0\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Cache-Control: no-cache
header: Pragma: no-cache
header: Expires: -1
header: Content-Type: text/html;charset=UTF-8
header: Content-Language: en
header: Transfer-Encoding: chunked
header: Connection: Close
header: Date: Tue, 03 Apr 2012 14:00:27 GMT
header: Server: WebSphere Application Server/7.0
Final url: http://myhost.mydomain:9080/am/home?rand=1333461626997
似乎在GET请求之后我出于某些原因获得了三次重定向。可能这个导致我无法访问我想要的URL:
send: 'GET /am/ve/login?dest=http%3A%2F%2Fmyhost.mydomain%3A9080%2Fam%2Fve%2Fmod%2Fconsole%2F&rand=1333461626691 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: myhost.mydomain:9080\r\nUser-Agent: Mozilla/5.0\r\nConnection: close\r\nCookie: LtpaToken2=AqDVFChifqCAOm3ZYllczyUV/3hWa8tcnQx6E1/E9bA9vkT+XMflFJjhm1X2WT6ZNrix5FUO34X8dLjxYMcCujlH16Gp0NevYHu3LUtDVdopOiLDIxbqrNX9aK1hhSQaivq1uyaboYJsivmUbvKHEFftg8IDieSv0Img5ytfkEFtSL8XxRlg0sSlEADlzltydg+m5cOeOaEcJWBkYeRdOZeK+KWp/3mBInWknReiUjuUKAmvawBqomGjqfEvJdLeSWfBcPyuiVleDoX5Ww8nLstntFK/17KrGMSzVaX+KDF9hMCkQWq6yZIU76nousJMXw5mAKXlWQI5K74QYTqM/mI6uxLq57CCZdBtHDaHRvFIiMWP2kWxQab0JOLwamYTWWoDo7Dq4NTUH2zAjUn/6Rb08Bwq5yB6LG3TYZFsItV8no7cZ59tmgCz/C8HyWVpilnuv8fF9UU++z9e9awwoo1gzdhRy3ZeippAmUGrKHiu3EN6vREE27oDVRNeIjIMfE26LDsuDLiZt+u2B12xR+kWKxCiPzSi00olR/2tfwoIBU6LU7fvs57OiNhOW9SrjinYeNrawbVBdcQQhsGRZhvrBmeW3N0oOjqVRerWAsQrTEneqrJ4P/gV6zLN4V6V; JSESSIONID=0000AioGqwi8QbBFL0Yt1h3wLq4:-1\r\nPragma: no-cache\r\nCache-Control: max-age=0\r\n\r\n'
我不知道如何摆脱它。欢迎任何建议。
答案 0 :(得分:0)
也许你应该使用某种日志隧道或其他东西(比如Apache的TcpTunnelGui)并检查你的浏览器在它工作时发送的确切内容(以及服务器返回)?然后在脚本中执行相同的操作。 :)