Results 1 to 4 of 4

Thread: QNetworkAccessManager strange problem and behavior

  1. #1
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo

    Default QNetworkAccessManager strange problem and behavior

    Hi all, I have strange problem with QNetworAccessManager.

    I was trying to make a web service application with Qt with OAuth.
    I manage to create my own OAuth service for my need based on QOAuth.
    But the problem is not in OAuth.
    The problem is in QNetworkAccessManager.


    For example, there is this url that produced by OAuth,
    if I open that url with QNetworkManager, like this
    Qt Code:
    1. QNetworkAccessManager manager = new QNetworkAccessManager();
    2. connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
    3.  
    4. // this is just for illustration not real consumer key and signature, the link produced by my OAuth
    5. QString url = "https://www.my-example-site.com/oauth/content?oauth_consumer_key=1234567890&oauth_nonce=1824974157&oauth_signature=aBAy4KWZcs5762N4VPjiDj%2FtyqY%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1366080188&oauth_version=1.0";
    6. QNetworkRequest request;
    7. request.setUrl( url );
    8. request.setRawHeader( "Content-Type", "application/x-www-form-urlencoded");
    9. QNetworkReply *reply = manager->get( request );
    To copy to clipboard, switch view to plain text mode 

    It gave error "Host Require Authentication", "Error 401", "oauth_problem=invalid_signature"

    but, if I generate new url with my OAuth and open that link on firefox, it will produce correct result.
    I also try libcurl for c++.
    Qt Code:
    1. CURL* curl;
    2.  
    3. curl_global_init(CURL_GLOBAL_ALL);
    4. curl = curl_easy_init();
    5.  
    6. // another illustration, not real consumer key and signature
    7. curl_easy_setopt(curl, CURLOPT_URL, "https://www.my-example-site.com/oauth/content?oauth_consumer_key=1234567890&oauth_nonce=1824974157&oauth_signature=a9Vy4+CZcs5762N4VPjiDj%2FtyqY%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1366080200&oauth_version=1.0");
    8. // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
    9. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    10.  
    11. curl_easy_perform(curl);
    12.  
    13. curl_easy_cleanup(curl);
    14. curl_global_cleanup();
    To copy to clipboard, switch view to plain text mode 
    It gave correct result and no error.

    I also try QHttp.
    Qt Code:
    1. QHttp http = new QHttp();
    2. connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));
    3. http->setHost("https://www.my-example-site.com", QHttp::ConnectionModeHttps);
    4. http->get("/oauth/content?oauth_consumer_key=1234567890&oauth_nonce=1824974157&oauth_signature=a9Vy4+ABCDEF62N4VPjiDj%2FtyqY%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1366080300&oauth_version=1.0");
    To copy to clipboard, switch view to plain text mode 
    It also gave correct result and no error.


    So, what is wrong with the way I use QNetworkAccessManager?
    I try it in qtcreator with Qt 4.8 and Qt 5, but no luck.
    They gave same error result.

    I want to keep on using QNetworkAccessManager, but if I can't resolve this problem, what should I do?
    Should I use curl or QHttp? or is there another network library that I can use?

    thank you.
    Last edited by creation; 16th April 2013 at 04:20.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager strange problem and behavior

    The first obvious difference is that you are issuing a GET but setting the content type to "application/x-www-form-urlencoded" which is typically only used for POST requests where a request body exists.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo

    Default Re: QNetworkAccessManager strange problem and behavior

    Quote Originally Posted by ChrisW67 View Post
    The first obvious difference is that you are issuing a GET but setting the content type to "application/x-www-form-urlencoded" which is typically only used for POST requests where a request body exists.
    Hi ChrisW67, thanks for reply.
    Originally, it was like this
    Qt Code:
    1. QByteArray authorizationHeader = paramsToString( *pm, ParseForHeaderArguments );
    2. request.setRawHeader( "Authorization", authorizationHeader );
    3. request.setRawHeader( "Content-Type", "application/x-www-form-urlencoded");
    4. // request.setRawHeader( "Content-Type", "text/html; charset=utf-8;");
    5. request.setRawHeader( "User-Agent", "My agent");
    To copy to clipboard, switch view to plain text mode 

    but when I comment all the lines, it still gave same error.
    I've tried many things from yesterday, but still no luck.
    I'm kind of out of idea here.
    Last edited by creation; 16th April 2013 at 05:15.

  4. #4
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo

    Default Re: QNetworkAccessManager strange problem and behavior

    I just figure out what happened.
    The problem is in url.

    I don't know how to explain correctly it, but I think QNetworkAccessManager has auto url encode function.

    for example if my signature contain "+" and I encode it with toPercentEncoding() it will become "%2B",
    When I call get(), it will be encoded again automatically by QNetworkAccessManager and become "%252B".
    And that's why it gave "oauth_problem=signature_invalid".

    So, I don't need to encode my url and this problem was solved and closed.

Similar Threads

  1. Qt process strange behavior
    By rspock in forum Newbie
    Replies: 5
    Last Post: 15th March 2013, 17:11
  2. setCellWidget - strange behavior
    By Archa4 in forum Newbie
    Replies: 5
    Last Post: 28th April 2011, 08:26
  3. QComboBox strange behavior
    By Antebios in forum Newbie
    Replies: 2
    Last Post: 31st March 2009, 23:50
  4. Strange resize behavior
    By Lykurg in forum Newbie
    Replies: 3
    Last Post: 9th January 2007, 13:56
  5. scrollbars strange behavior
    By siniy in forum Qt Programming
    Replies: 6
    Last Post: 29th December 2006, 10:27

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.