PostHeaderIcon Qt http get & post

I need a tool to change the get request url to post method like a post form, so I wrote it with Qt.

it can be implemented by javascript,  many languages type of json, python, maybe a plugin in firefox or chrome. I know this, but I still write it. maybe for the next small tool.

Qt is good for GUI application development, but the network utils of it is a little bad, on the official API document, few examples about http header configuration and network request configuration, especially, almost no mention of Asian language related settings.

So, let’s begin with the small tool.

1.GUI of it

2.function

a. do a http get with no paras;

b. do a http post with paras;

c. U have to set the charset of your request page;

d. the response or the reply from the server will display in the result text area.

3.key points

a. get the input data;

b. process the data with QNetworkAccessManager, QNetworkRequest andQNetworkReply,the more detail examples of these three U can visit Creating an HTTP network request in Qt using QNetworkAccessManager, QNetworkRequest, QNetworkReply on Nokia developer, I didn’t download it.

c. show the server response.

4.Code Snippets

a. declare QNetworkAccessManager.

    QNetworkAccessManager *nam;
    // some code
    nam = new QNetworkAccessManager(this);
    // some code

b. request and reply.

    // request
    QByteArray post_data;
    // convert Unicode to 8-bit char
    post_data.append(paras.toAscii());
    QNetworkRequest request= QNetworkRequest(QUrl(url));
    // request
    request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    if(post_data.isEmpty())
    {
        nam->get(request);
    }
    else
    {
        nam->post(request,post_data);
    }

    // reply
    if(reply->error() == QNetworkReply::NoError)
    {
        ui->textEdit_result->setText(QObject::tr(reply->readAll()));
    }
    else
    {
        ui->textEdit_result->setPlainText(reply->errorString());
    }
    // some code

c. connect the reply with the signal.

    connect(nam,
            SIGNAL(finished(QNetworkReply*)),
            this,
            SLOT(finished(QNetworkReply*)));

d. output

5.Source Code

donwload http post.

6.Other

qt display charset set in context:

QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));

I do not know how to get the charset from the reply header yet, I will learn it and update here.

171,319 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com