QT post/get Send/Get web page data

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.