#include <QNetworkReply>#include <QNetworkRequest>QNetworkAccessManager *m_accessManager;
Widgets::Widgets (Qwidget *parent):Qwidget (parent), UI (newUi::Widget) {ui->setupui (this); M_accessmanager = newQnetworkaccessmanager (this);Qobject::connect (M_accessmanager,SIGNAL (Finished (qnetworkreply*)), this,SLOT (Finishedslot (qnetworkreply*)));}widget::~Widget () {Delete UI;}voidWidget::finishedslot (Qnetworkreply *reply) {if (reply->error () = =Qnetworkreply::NOERROR) {Qbytearray bytes = Reply->readall (); Qdebug () <<bytes;QString string =Qstring::fromutf8 (bytes); Ui->textbrowser->settext (String.toutf8 ()); }else {qdebug () <<"Handle Errors Here";Qvariant Statuscodev = Reply->attribute (Qnetworkrequest::Httpstatuscodeattribute); Statuscodev isThe corresponding code for the HTTP server, Reply->error () isQT Definition error code, can refer toQT Document Qdebug ("Found error .... Code:%d%d\n", Statuscodev.toint (), (int) reply->error ()); Qdebug (Qprintable (reply->errorstring ())); } reply->deletelater ();}voidWidget::on_pushbutton_clicked () {//POSTQnetworkrequest *request = newQnetworkrequest (); Request->seturl (Qurl ("Http://localhost:8888/login"));//Request->setheader (Qnetworkrequest::Contenttypeheader,"application/x-www-form-urlencoded");//Request->setrawheader ("Accept","Text/html, Application/xhtml+xml, */*");//Request->setrawheader ("Referer","Http://localhost:8888/login");//Request->setrawheader ("Accept-language","ZH-CN");//Request->setrawheader ("X-requested-with","XMLHttpRequest");//Request->setrawheader ("User-agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; trident/5.0) ");//Request->setrawheader ("Content-type","application/x-www-form-urlencoded");//Request->setrawheader ("Accept-encoding","Gzip,deflate");//Request->setrawheader ("Host","localhost:8888");//Request->setrawheader ("Content-length","(");//Request->setrawheader ("Connection","Keep-alive");//Request->setrawheader ("Cache-control","No-cache");Qbytearray PostData; Postdata.append ("myname=lk&mypwd=33"); qnetworkreply* reply = M_accessmanager->post (*request,postdata);} void widget::on_pushbutton_get_clicked () {//get// qurl url ("https://kyfw.12306.cn/otn/ Resources/js/framework/station_name.js ");// qurl URL (" Http://localhost:8888/login ");// qurl URL (" Http://dict.baidu.com/s?wd=name "); qnetworkrequest *request = new qnetworkrequest (); Request->seturl (qurl ("http:// Dict.baidu.com/s?wd=name ")); M_accessmanager->get (*request);}
http://blog.csdn.net/liukang325/article/details/46915359
QT post/get Send/Get web page data