我一直在尝试这一天,但没有成功。请帮助解决问题。在谷歌搜索中,我发现许多用户都有这个问题,但我找不到解决方案。
我正在尝试在 QT C++ 中进行 HTTP 发布,并且我已经尝试在 python 中执行此操作(我的问题不是 python 问题,所以 Qt 专业人士请帮忙).. 我知道,我在处理 cookie 和所有内容方面有问题,所以请帮忙。请提供可能的解决方案。
在python中,代码简洁明了。我已经剥离了错误处理和所有额外的东西以使其变得简单。
url = 'http://www.example.com/'
data = 'username=abc&password=passwd'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
usock = opener.open(url, data)
#>>>>>> NOW, I have the cookiejar <<<<<<<<<
opener.addheaders = [('Referer','http://www.example.com/xyz.php'),('User-Agent','Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0')]
data_to_send = 'ABCDEFGH'
url_send = "http://www.example.com/xyz.php"
send = opener.open(url_send,data_to_send)
我制作的 QT 等效项:-
void SmsSender::sendToMyCantos()
{
manager = new QNetworkAccessManager(this);
manager->setCookieJar(new QNetworkCookieJar(manager));
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
request.setUrl(QUrl("http://www.mycantos.com"));
postData.append("username=abc&password=passwd");
manager->post(request,postData);
//>>>>>> So, I feel that I have CookieJar now to make POST <<<<<<<
request.setRawHeader("Referer","http://www.example.com/xyz.php");
request.setRawHeader("User-Agent","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0");
postData.clear();
postData.append("ABCDEFGH");
request.setUrl(QUrl("http://www.example.com/xyz.php"));
manager->post(request,postData);
}
现在的问题是我无法在 QT 中做同样的事情。我面临的问题:
- 处理 cookie
- 处理重定向 (HTTP 302)
- 保留 cookie 以进行未来的 POST
所有这些都是在 python 中自动完成的。下面,代码没有直接关系,但我对此进行了编码以允许在 POST 中进行重定向。代码与我用来制作它的链接非常相似。
QUrl SmsSender::redirectUrl(const QUrl& possibleRedirectUrl,
const QUrl& oldRedirectUrl) const {
//Checking infinite resursions
QUrl redirectUrl;
if(!possibleRedirectUrl.isEmpty() &&
possibleRedirectUrl != oldRedirectUrl) {
redirectUrl = possibleRedirectUrl;
}
return redirectUrl;
}
void SmsSender::replyFinished(QNetworkReply *reply)
{
QVariant possibleRedirectUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
QVariant data_size = reply->header(QNetworkRequest::ContentLengthHeader);
qDebug()<<data_size.toFloat();
qDebug()<<manager->cookieJar()->cookiesForUrl(QUrl("http://www.example.com"));
/* We'll deduct if the redirection is valid in the redirectUrl function */
_urlRedirectedTo = this->redirectUrl(possibleRedirectUrl.toUrl(),
_urlRedirectedTo);
/* If the URL is not empty, we're being redirected. */
if(!_urlRedirectedTo.isEmpty()) {
QString text = QString("SmsSender::replyFinished: Redirected to ")
.append(_urlRedirectedTo.toString());
qDebug(text.toAscii());
// Do again in case we have more redirections
this->_qnam->get(QNetworkRequest(_urlRedirectedTo));
}
else
{
QString text = QString("SmsSender::replyFinished: Arrived to ")
.append(reply->url().toString());
qDebug(text.toAscii());
_urlRedirectedTo.clear();
}
}
QNetworkAccessManager* SmsSender::createQNAM() {
QNetworkAccessManager* qnam = new QNetworkAccessManager(this);
/* We'll handle the finished reply in replyFinished */
connect(qnam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
return qnam;
}