发送添加了用户代理的QHTTP请求
C:\ Python33 \ Lib \ site-packages \ PyQt4 \ examples \ network \ http
中有一个例子但我想将User-Agent标头添加到http请求,所以我评论了
## self.httpGetId = self.http.get(path, self.outFile)
并编写以下代码
header = QtNetwork.QHttpRequestHeader("GET", path)#1
header.setValue("Host", url.host())
header.setValue('User-Agent' ,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17')
self.http.setHost(url.host())
然后这是问题,我不知道如何发送此请求并添加到服务器的标头?
P.S。 我参考了文档,或许找到了
int QHttp::request ( const QHttpRequestHeader & header, QIODevice * data = 0, QIODevice * to = 0 )
会帮助我解决这个问题,但是我想知道在这种情况下'数据'应该是什么?
答案 0 :(得分:1)
使用QNetworkAcessManager
发送您的请求,如下所示:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtCore, QtNetwork
if __name__ == '__main__':
import sys
print "Starting request...",
app = QtCore.QCoreApplication(sys.argv)
url = QtCore.QUrl("http://www.example.com/")
request = QtNetwork.QNetworkRequest()
request.setUrl(url)
request.setRawHeader("User-Agent", "MyAppName/1.0 (Nokia; Qt)")
manager = QtNetwork.QNetworkAccessManager()
manager.finished.connect(app.quit)
manager.get(request)
print "done."
sys.exit(app.exec_())