如何使用Qt 4.6.1创建HTTP POST请求?

时间:2022-03-08 20:16:04

How can I create a HTTP POST request with some URL encoded parameters using Qt 4.6.1?

如何使用Qt 4.6.1使用URL编码参数创建HTTP POST请求?

I figured out that I can create a QNetworkRequest, set all the parameters there and send it via QNetworkAccessManagers post method. But how can I add some URL-encoded parameters to the request?

我发现我可以创建一个QNetworkRequest,在那里设置所有的参数并通过qnetworkaccessmanager post方法发送它。但是如何向请求添加url编码的参数呢?

In the end I want to access the Eve API using Qt/C++. A Python example can be found here: http://www.eveonline.com/api/doc/example-python.asp

最后,我希望使用Qt/ c++访问Eve API。在这里可以找到一个Python示例:http://www.eveonline.com/api/doc/exam-python.asp


I managed it using something like (still to be refactored and formed into something useful):

我使用了一些东西来管理它(仍然需要重构并形成一些有用的东西):

QNetworkReply *requestApi(QNetworkAccessManager &nwam)
{

    QNetworkRequest request(QUrl("http://api.eve-online.com/account/Characters.xml.aspx"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");

    QByteArray data;
    QUrl params;

    params.addQueryItem("userid","user");
    params.addQueryItem("apiKey","key");
    data.append(params.toString());
    data.remove(0,1);

    QNetworkReply *reply = nwam.post(request,data);
    return reply;
}

2 个解决方案

#1


11  

Your solution is almost right. But one should use:

你的解决方案几乎是对的。但是每个人都应该使用:

data = params.encodedQuery();

instead of

而不是

data.append(params.toString());
data.remove(0,1);

to handle UTF8 strings properly.

正确处理UTF8字符串。

#2


6  

I'm sorry that I only find your post this late. However, I'll still try to help, in case anyone else is searching for the answer.

很抱歉我这么晚才找到你的邮件。不过,我还是会尽力帮忙,以防有人在寻找答案。

By accident, I'm also working on an EVE API application, and I also tried the same way. Unfortunately, QNetworkManager doesn't work that way, because it posts the request asynchronously. You have to connect a slot to its finished(QNetworkReply*) signal.

偶然地,我也在使用EVE API应用程序,我也尝试了同样的方法。不幸的是,QNetworkManager不是这样工作的,因为它异步地发布请求。你必须连接一个插槽到它的完成(QNetworkReply*)信号。

I do it by making a request with a separate class called EveConnector, processing the reply in the slot connected to the QNetworkManager's finished signal, and then calling back the requesting object through the connector class's own signals.

我使用一个名为EveConnector的单独的类进行请求,处理与QNetworkManager完成的信号连接的槽中的应答,然后通过连接器类自己的信号调用请求对象。

I would happily share the code, if you ask.

如果你问的话,我很乐意分享这些代码。

#1


11  

Your solution is almost right. But one should use:

你的解决方案几乎是对的。但是每个人都应该使用:

data = params.encodedQuery();

instead of

而不是

data.append(params.toString());
data.remove(0,1);

to handle UTF8 strings properly.

正确处理UTF8字符串。

#2


6  

I'm sorry that I only find your post this late. However, I'll still try to help, in case anyone else is searching for the answer.

很抱歉我这么晚才找到你的邮件。不过,我还是会尽力帮忙,以防有人在寻找答案。

By accident, I'm also working on an EVE API application, and I also tried the same way. Unfortunately, QNetworkManager doesn't work that way, because it posts the request asynchronously. You have to connect a slot to its finished(QNetworkReply*) signal.

偶然地,我也在使用EVE API应用程序,我也尝试了同样的方法。不幸的是,QNetworkManager不是这样工作的,因为它异步地发布请求。你必须连接一个插槽到它的完成(QNetworkReply*)信号。

I do it by making a request with a separate class called EveConnector, processing the reply in the slot connected to the QNetworkManager's finished signal, and then calling back the requesting object through the connector class's own signals.

我使用一个名为EveConnector的单独的类进行请求,处理与QNetworkManager完成的信号连接的槽中的应答,然后通过连接器类自己的信号调用请求对象。

I would happily share the code, if you ask.

如果你问的话,我很乐意分享这些代码。