[Solved]send QHTTP request with user agent added



  • there is an example in C:\Python33\Lib\site-packages\PyQt4\examples\network\http

    but I want to add User-Agent header to the http request ,so I commented
    @

    self.httpGetId = self.http.get(path, self.outFile)

    @

    and write the following code
    @
    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())
    

    @

    then here is the question ,I don’t know how to Send this request with the header added to the server?

    P.S.
    I referred the documentation ,found perhaps
    int QHttp::request ( const QHttpRequestHeader & header, QIODevice * data = 0, QIODevice * to = 0 )
    would come to help me solve this question ,but I wonder what ‘data’ should be in this case ?


  • Moderators

    You should use "QNetworkAccessManager":http://qt-project.org/doc/qt-4.8/qnetworkaccessmanager.html and it's post()/put()/get() methods instead of QHttp.

    @
    QNetworkRequest *req(QUrl("http://server.com"));
    req->setRawHeader("User-Agent" ,"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17");

    QNetworkReply* reply = m_NAM->post(req,DATA);
    @



  • why does reply.rawHeaderPairs () ==[] here ?
    is there something wrong ?
    @
    import sys
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from PyQt4.QtNetwork import *

    if name == 'main':

    app =QCoreApplication(sys.argv)
    
    manager=QNetworkAccessManager ()
    url =input('input url :')
    
    print(QUrl.fromEncoded(QUrl(url).toEncoded()))
    request=QNetworkRequest (QUrl.fromEncoded(QUrl(url).toEncoded()))
    
        
    request.setRawHeader("User-Agent" ,'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1')
    reply = manager.head(request)
    
    print(reply.rawHeaderPairs () )
    sys.exit()
    

    @
    [quote author="raven-worx" date="1365750066"]You should use "QNetworkAccessManager":http://qt-project.org/doc/qt-4.8/qnetworkaccessmanager.html and it's post()/put()/get() methods instead of QHttp.

    @
    QNetworkRequest *req(QUrl("http://server.com"));
    req->setRawHeader("User-Agent" ,"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17");

    QNetworkReply* reply = m_NAM->post(req,DATA);
    @[/quote]


  • Moderators

    [quote author="redstoneleo" date="1366007227"]why does reply.rawHeaderPairs () ==[] here ?
    is there something wrong ?
    [/quote]
    Because you setting the raw header on the request and checking the reply for the raw header afterwards? :)

    Or in case you are wondering why the reply doesn't contain anything at all:
    You don't give the reply a chance to receive any data. You need to listen to the finished signal of the reply. The Headers are coming (mostly) with the first chunk of data from the server.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.