Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [Solved]send QHTTP request with user agent added

    Language Bindings
    2
    4
    4312
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      redstoneleo last edited by

      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 ?

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        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);
        @

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 0
        • R
          redstoneleo last edited by

          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]

          1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            [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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • First post
              Last post