【问题标题】:How to use post() in qt?如何在 qt 中使用 post()?
【发布时间】:2012-06-12 08:00:07
【问题描述】:

这是我的程序。在这个程序中,我想向网站发送请求(例如:http://www.adobe.com/products/muse.html
我想在纯文本框中显示返回给我的 html 代码。

QUrl url("http://www.adobe.com/products/muse.html")

我想在“thisfile”中给出html代码

file.setFileName("thisfile.html");

if (!file.open(QIODevice::WriteOnly))
{
    std::cerr << "Error: Cannot write file "
    << qPrintable(file.fileName()) << ": "
    << qPrintable(file.errorString()) << std::endl

    return false;
}

http.setHost(url.host(),80);
http.post(url.toString(),"term=yyyy&loc=en_us&siteSection=products%3Amuse",&file);

此代码无法正常工作,当我显示文件时给我错误的 html 代码。我该怎么办?

【问题讨论】:

  • file 实例的范围是什么?它是那个街区的本地人吗?

标签: qt post


【解决方案1】:

使用http.get() 代替http.post() 作为POST 方法需要设置服务器使用的其他Headers

QHttp::get() 方法也是异步

由于您的情况很简单,只需检索HTML 响应,您应该选择HTTP GET 恕我直言。 See difference between GET and POST method.

如果你只能使用HTTP POST,那么check this

【讨论】:

  • 感谢您的重播,我这样做之前:将 http.post() 行中的 url.toString() 替换为 url.path() 但不使用完整
  • 我必须在我的项目中使用 post
  • 我已经更新了我的答案。它为http://www.adobe.com/products/muse.html返回正确的html。
【解决方案2】:
 QNetworkRequest request;
 request.setUrl(QUrl("thisfile.html"));

 QNetworkReply *reply = manager->post(request, "term=yyyy&loc=en_us&siteSection=products%3Amuse");
 connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));

查看qt docs的QNetworkAccessManager

您必须阅读信息并将其保存到readyRead函数的文件中

【讨论】:

  • 感谢您的重播,但我有一个问题:我必须在哪里设置文件(&file)的地址,以便将它发送给我的 savig html cod?
  • 喜马请帮帮我,重播给我
  • 在我的示例中,您必须在插槽“slotReadyRead”处实现文件操作。
猜你喜欢
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多