Results 1 to 20 of 29

Thread: Reqest and Response in XML

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Reqest and Response in XML

    Hi

    I am working on a project that uses Basic authentication scheme to authenticate a user.

    I need to call a api in a website.I need to send the username and password in base64 format.

    After the header has the host and authentication details, I need to send the request in this format.

    === Request ===

    <request client_id="1" client_serial_number="abcded" version="0.1">
    <call name="test.reverse">abc</call>
    </request>

    This is the responce that i should get if all goes well

    === Response ===

    <response version="0.1">
    <value>cba</value>
    </response>

    For the header request part, i am using the following code

    Qt Code:
    1. QXmlStreamReader xml;
    2. QHttp http;
    3.  
    4. void senddata::fetch()
    5. {
    6.  
    7. QString username = "abc";
    8. QString password = "12345";
    9.  
    10. xml.clear();
    11.  
    12. QUrl url(lineEdit->text());
    13.  
    14. http.setHost(url.host());
    15. http.setUser(username,password);
    16. connectionId = http.get(url.path());
    17. }
    To copy to clipboard, switch view to plain text mode 
    I am reading the data here

    Qt Code:
    1. void senddata::readData(const QHttpResponseHeader &resp)
    2. {
    3. if (resp.statusCode() != 200)
    4. http.abort();
    5. else {
    6. xml.addData(http.readAll());
    7. parseXml();// This funtion has nothing in it as of now
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Is my code correct? How should i proceed?

    How can i parse the response xml and get the data i need?

  2. #2
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    can somebody help me with this??

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    For such simple things I would use QDomDocument instead of the stream reader.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    thanks for replying mate.

    I think what u suggested is what i need for proper functioning

    abt headers and data, how can I send the data along with the header? I am stuck here.

    Can QDomDocument also manage the response as well as the QHttpResponseHeader?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    QDomDocument is for parsing xml data, it has nothing to do with HTTP.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    thanks mate.

    can somebody guide me as how i can send the data along with my header??

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    Did you notice QHttp::post() and QHttp::request()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    ok.

    int QHttp::request ( const QHttpRequestHeader & header, QIODevice * data = 0, QIODevice * to = 0 )

    can you explain the function in a better way, than the explanation that is present in Qt Documentation?

    what should i assign in the QHttpRequestHeader and in QIODevice?

    this is the request header

    Qt Code:
    1. QByteArray string ("username");
    2. string.append(":");
    3. string.append("12345");
    4. QString converted = string.toBase64();
    5. //qDebug() <<"String"<< string<<"converted" <<converted ;
    6.  
    7. QUrl url(lineEdit->text());
    8.  
    9. http.setHost(url.host());
    10.  
    11. connectionId = http.get(url.path());
    To copy to clipboard, switch view to plain text mode 

    this is the data that has to be sent along with the header.

    Qt Code:
    1. void MainWindow::sendrequest()
    2. {
    3. QDomDocument doc("request");
    4. QDomElement root = doc.createElement("request");
    5. doc.appendChild(root);
    6.  
    7. QDomElement tag = doc.createElement("call");
    8. root.appendChild(tag);
    9.  
    10. QDomText t = doc.createTextNode("abc");
    11. tag.appendChild(t);
    12.  
    13. QString xml = doc.toString();
    14. }
    To copy to clipboard, switch view to plain text mode 

    can u help me as how I should call the request function??

    Will using QNetworkAccessManager for the request and response from a webpage, be easier?

    thanks
    Last edited by srohit24; 1st July 2009 at 15:46.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    "data" is a pointer to a QIODevice (such as QBuffer) containing the data the engine should send to the server as the request (i.e. the result of your sendrequest() method). As for the header - the least you have to provide is the method (GET or POST) and relative path of the URL that should be queried.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    srohit24 (1st July 2009)

  11. #10
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    thanks mate.

    so i need to call the request, like this?
    http.request( GET, xmlrequest);

    I need to have the xml request like this

    <request client_id="1" client_serial_number="abcded" version="0.1">
    <call name="test.reverse">abc</call>
    </request>

    but after using this code,

    Qt Code:
    1. QDomDocument doc("request");
    2. QDomElement root = doc.createElement("request");
    3. doc.appendChild(root);
    4.  
    5. QDomElement tag = doc.createElement("call");
    6. QString name = "teci.test.reverse";
    7. QString value = "abc";
    8. tag.setAttribute(name,value);
    9. root.appendChild(tag);
    10.  
    11. QString xmlrequest = doc.toString();
    To copy to clipboard, switch view to plain text mode 

    i am able to only this.

    "<!DOCTYPE request>
    <request>
    <call test.reverse="abc" />
    </request>

    can you help me with this?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    Quote Originally Posted by srohit24 View Post
    thanks mate.

    so i need to call the request, like this?
    http.request( GET, xmlrequest);
    Not really, you have to match the argument types (and values of course).

    I need to have the xml request like this

    <request client_id="1" client_serial_number="abcded" version="0.1">
    <call name="test.reverse">abc</call>
    </request>

    but after using this code,

    Qt Code:
    1. QDomDocument doc("request");
    2. QDomElement root = doc.createElement("request");
    3. doc.appendChild(root);
    4.  
    5. QDomElement tag = doc.createElement("call");
    6. QString name = "teci.test.reverse";
    7. QString value = "abc";
    8. tag.setAttribute(name,value);
    9. root.appendChild(tag);
    10.  
    11. QString xmlrequest = doc.toString();
    To copy to clipboard, switch view to plain text mode 

    i am able to only this.

    "<!DOCTYPE request>
    <request>
    <call test.reverse="abc" />
    </request>
    What more would you expect from the above code snippet?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    i want to exact copy of the request xml thats been given.

    I am asking, what changes should i make to get the xml that will request the server.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.