我想向应该以 .csv 文件响应的 HTTPS 站点发出发布请求。我有这个 Python 代码:
url = 'https://www.site.com/servlet/datadownload'
values = {
'val1' : '123',
'val2' : 'abc',
'val3' : '1b3',
}
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
response = urllib2.urlopen(req)
myfile = open('file.csv', 'wb')
shutil.copyfileobj(response.fp, myfile)
myfile.close()
但是'我得到了错误:
BadStatusLine: '' (in httplib.py)
我已经尝试使用 Chrome 扩展程序发布请求:高级 REST 客户端(屏幕截图),效果很好。
可能是什么问题,我该如何解决?(是因为HTTPS吗?)
编辑,重构代码:
try:
#conn = httplib.HTTPSConnection(host="www.site.com", port=443)
=> 给出一个BadStatusLine: ''
错误
conn = httplib.HTTPConnection("www.site.com");
params = urllib.urlencode({'val1':'123','val2':'abc','val3':'1b3'})
conn.request("POST", "/nps/servlet/exportdatadownload", params)
content = conn.getresponse()
print content.reason, content.status
print content.read()
conn.close()
except:
import sys
print sys.exc_info()[:2]
输出:
Found 302
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>302 Found</TITLE>
</HEAD><BODY>
<H1>Found</H1>
The document has moved <A HREF="https://www.site.com/nps/servlet/exportdatadownload">here</A>.<P>
<HR>
<ADDRESS>Oracle-Application-Server-10g/10.1.3.5.0 Oracle-HTTP-Server Server at mp-www1.mrco.be Port 7778</ADDRESS>
</BODY></HTML>
我究竟做错了什么?