使用Python模拟登录Z-Blog系统的后台时,如果构造的header信息如下,那么得到的结果将会乱码,如下图所示
对应的错误的header信息如下:
url = "http://192.168.1.11/cmd.asp?act=verify"
request = urllib2.Request(url)
request.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0")
request.add_header("Host","192.168.1.11") request.add_header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
request.add_header("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3")
request.add_header("Accept-Encoding","gzip, deflate")
request.add_header("Referer","http://192.168.1.11/login.asp")
request.add_header("Cookie","ASPSESSIONIDQABAQQRA=; username=admin; password=XXX; ASPSESSIONIDQCBBQQQB=XXX")
request.add_header("Connection","keep-alive")
f = urllib2.urlopen(request)
print f.read()
问题处在:
request.add_header("Accept-Encoding","gzip, deflate")
只需删除该条header信息即可
具体原因,还没细看,先解决问题,后面再好好研究下