简体   繁体   English

如何使用Qt 5将数据发布到服务器?

[英]How to POST data to a server with Qt 5?

I'm trying to send a bit of json data to a web service using Qt 5.1.1. 我正在尝试使用Qt 5.1.1向Web服务发送一些json数据。 There are many examples out there, but almost all are for Qt 4.x, which had a slightly different API. 有很多示例,但是几乎所有示例都是针对Qt 4.x的,它的API略有不同。 Here's what I'm trying now: 这是我现在正在尝试的方法:

QUrl url("http://...");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QByteArray jsonInPostFormat = "json=" + QUrl::toPercentEncoding(jsonAsString);
jsonInPostFormat.replace("%20","+");
reply = net->post(request, jsonInPostFormat);
connect(reply, SIGNAL(finished()),   this, SLOT(finishedAddComment()));

The above has ... no effect. 以上没有效果。 If I'm doing things correctly, I seem to not get any response from the server. 如果我做的事情正确,则服务器似乎没有任何响应。 (I call reply->readALL() in finishedAddComment().) (我在completedAddComment()中调用reply-> readALL()。)

The service has a test form that works correctly. 该服务具有正常运行的测试表格。 I've dumped the headers that it's sending and looked at the html, which is just a standard form with post method specified. 我已经转储了要发送的标头,然后查看了html,这只是指定了post方法的标准形式。

Am I doing something obviously wrong? 我做错了什么吗? Is there a better way, say, using QUrlQuery? 比方说,有没有使用QUrlQuery的更好方法?

Some way of seeing the raw HTML requests that Qt is actually sending would be super-helpful... 查看Qt实际发送的原始HTML请求的某种方式非常有用...

Thanks! 谢谢! Tyler 泰勒

Qt5 no longer has the QUrl::encodedQuery() method. Qt5不再具有QUrl :: encodedQuery()方法。 Not sure, but from the documentation it might work using QUrl::query() method instead. 不确定,但是从文档中可以使用QUrl :: query()方法代替。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM