×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    May 2009
    Posts
    50

    qt http post request with authentication

    Hello, is there any example for how to make post request to the authenticated web site. I want to do same thing with below code segment implemented in java for Qt application. I just want to implement http post request with basic authentication in Qt.

    String userName = "myusername";
    String password = "mypassword";


    String cridentials = new sun.misc.BASE64Encoder().encode((userName + ":" + password).getBytes());

    // Send data

    URL url = new URL("Mywebsite");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty ("Authorization", "Basic " + cridentials);
    String encodedData = URLEncoder.encode("Test Symbian!..", "UTF-8");

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write("status=" + encodedData);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
    System.out.println(line);
    // Process line...
    }
    wr.close();
    rd.close();

  2. #2
    Nokia Developer Moderator
    Join Date
    Sep 2007
    Posts
    121

    Re: qt http post request with authentication

    Authorization or any other header can be added to QNetworkRequest or QHttpRequestHeader objects. Below small code example how to add the Authorization header to QNetworkRequest and post the request via QNetworkAccessManager.
    Code:
    void HTTPService::doPost(QString url, QString userName, QString password, QString data)
    {
    QString credentials = userName + ":" + password;
    credentials = "Basic " + credentials.toAscii().toBase64();
    
    QUrl qurl(url);
    QNetworkRequest request(qurl);
    
    request.setRawHeader(QByteArray("Authorization"),credentials.toAscii());
    
    //m_nManager is QNetworkAccessManager object
    //currentDownload is ptr to QNetworkReply
    m_currentDownload = m_nManager.post(request,"data=" + QUrl::toPercentEncoding(data));
    
    //pageReady function gets called when HTTP request is completed
    connect(m_currentDownload, SIGNAL(finished()),SLOT(pageReady()));
    }
    As said earlier another option is to use QHttpHeader and QHttp classes.

  3. #3
    Regular Contributor
    Join Date
    May 2009
    Posts
    50

    Re: qt http post request with authentication [SOLVED]

    Here is my working example if anyone needed:

    QNetworkAccessManager* netMan;
    netMan = new QNetworkAccessManager(this);
    QNetworkRequest request;


    request.setUrl(QUrl("Request Url"));
    request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg("Username").arg("Password").toAscii()).toBase64());

    QDateTime currTime = QDateTime::currentDateTime();
    QString timeData = currTime.toString("MM/dd/yyyy hh:mm:ss");
    QString mySt = status + " " + timeData;
    QByteArray myData(mySt.toUtf8(),mySt.length());

    QNetworkReply* reply = netMan->post(request,myData);

Similar Threads

  1. Simple http request
    By Christophe.A in forum Symbian Networking & Messaging (Closed)
    Replies: 8
    Last Post: 2008-06-11, 16:42
  2. Problem with HTTP POST
    By thathoo in forum Mobile Java General
    Replies: 2
    Last Post: 2006-12-18, 18:23
  3. Problems with HTTP POST
    By Summerman in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2006-11-27, 09:25
  4. Custom HTTP Header Name in POST Request
    By srigans1 in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2006-08-25, 06:56
  5. 7650 J2ME/MIDP HTTP Post
    By cwalkinshaw in forum Mobile Java General
    Replies: 2
    Last Post: 2002-10-23, 10:12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •