How to use http cookies with Qt
2014-01-20 16:55
495 查看
ps:以下文章中文部分为软件翻译的结果,看的时候请自行分析
How to use http cookies with Qt
Overview
原文:A cookie, also known as a web cookie, browser cookie, and HTTP cookie, is a piece of text stored by a user's web browser. A cookie can be used for authentication, storing site preferences, shopping cart contents, the identifier for a server-based session,
or anything else that can be accomplished through storing text data.A cookie consists of one or more name-value pairs containing bits of information, which may be encrypted for information privacy and data security purposes. The cookie is sent as an HTTP header
by a web server to a web browser and then sent back unchanged by the browser each time it accesses that server.
翻译:
一cookie,也被称为一个Web浏览器的cookie,cookie和HTTP,是一块存储在用户浏览器的文本。Cookie可以用于认证,存储网站的喜好,购物车的内容,为基于服务器的会话标识符,或其他任何可以通过存储文本数据来完成的。一个cookie包含一个或多个名称-值对包含的信息,它可以被加密的信息隐私和数据安全的目的。Cookie是由Web服务器网页浏览器HTTP标头发送然后送回不变的浏览器,每次访问服务器。
Code
原文:The Qt Network module offers the class QNetworkCookieJar which can be used with QNetworkAccessManager to manage cookies in Qt applications.The following class will show you how to send a post request to a URL and read the cookies stored in the header.
翻译:
QT网络模块提供的类qnetworkcookiejar可与qnetworkaccessmanager在Qt应用程序管理的cookies。下面的类将告诉你如何发送POST请求的URL和读取存储在头的cookie。
#ifndef COOKIESHANDLER_H #define COOKIESHANDLER_H #include <QObject> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkCookieJar> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> #include <QDebug> class cookiesHandler: public QObject{ Q_OBJECT public: cookiesHandler(QObject *parent = 0) : QObject(parent){ mManager = new QNetworkAccessManager(this); mManager->setCookieJar(new QNetworkCookieJar(this)); connect(mManager, SIGNAL(finished(QNetworkReply*)), SLOT(replyFinished(QNetworkReply*))); } void sendPostRequest(const QUrl &url, const QByteArray &data){ mUrl = url; QNetworkRequest r(mUrl); mManager->post(r, data); } virtual ~cookiesHandler(){} private slots: void replyFinished(QNetworkReply *reply){ if (reply->error() != QNetworkReply::NoError){ qWarning() << "ERROR:" << reply->errorString(); return; } QList<QNetworkCookie> cookies = mManager->cookieJar()->cookiesForUrl(mUrl); qDebug() << "COOKIES for" << mUrl.host() << cookies; } private: QNetworkAccessManager *mManager; QUrl mUrl; }; #endif // COOKIESHANDLER_H The previous class can be used as follow: QUrl url("https://yourwebsite.com"); // https and http are both allowed. QByteArray postData; postData.append("username=myUsername&password=myPAss&hiddenFields=ifAny"); c.sendPostRequest(url, postData); The cookie can be sent back to the server with another request in this way: QList<QNetworkCookie> cookies = mManager->cookieJar()->cookiesForUrl(mUrl); QVariant var; var.setValue(cookies); QNetworkRequest r(QUrl("http://www.developer.nokia.com/Profile/")); r.setHeader(QNetworkRequest::CookieHeader, var); mManager->get(r);
相关文章推荐
- How to use http cookies with Qt
- How To Use Proxy Server To Access Internet at Shell Prompt With http_proxy Variable. [reprint]
- [转]How to use IHttpContextAccessor in static class to set cookies
- How to use HttpSendRequestEx with password-protected URLs
- (转)How to use HttpSendRequestEx with password-protected URLs
- how to use Wordnet with Java?
- (转)How to use MySQL with 3D Studio Max
- How to use Access Log Sampler to Generate Traffic with JMeter
- Qt How to use connect between incompatible signal and slot
- How To Set Up HTTP Authentication With Nginx On Ubuntu 12.10
- How to configure and use Git with visual studio 2012 and TFS
- How to use btt with blktrace to analysis the trace file
- How to upload images with text using iPhone SDK using ASIHttpRequest ?
- How to create a DLL library in C and then use it with C#
- CSS3的calc()使用(转载自 http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html)
- how to use the mathmatical constant "e" in conjunction with a vector
- How To Compile Qt with Visual Studio 2010
- How to use Qt’s QSortFilterProxyModel
- How to use InterfaceInterceptor of Unity with configuration file
- [ZZ]How To Compile Qt 4.7 with Visual Studio 2010