如何在Qt中使用post()? [英] How to use post() in qt?

查看:777
本文介绍了如何在Qt中使用post()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my program. In this program I want to send request to a website (for example: http://www.adobe.com/products/muse.html)
I want to show the html code that return me in plain text box.

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

我想在此文件"中提供html代码

I want to give html code in "thisfile"

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代码.我该怎么办?

This code doesn't work correctly and when I show the file give me false html code. What do I have to do?

推荐答案

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

Use http.get() instead of http.post() as POST method requires to set other Headers used by server.

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

由于您的情况很简单,只需要检索HTML响应,因此您应该进行HTTP GET恕我直言.

As your case is simple enough just to retrieve HTML response, you should go for HTTP GET IMHO. See difference between GET and POST method.

如果仅需使用HTTP POST,则 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆