我一直在嘗試這一整天都沒有成功。請幫助解決問題。在谷歌搜索我發現很多用戶有這個問題,但我找不到解決方案。QT HTTP當服務器需要cookies時發佈問題
我試圖做的HTTP發佈的QT C++ &我已經嘗試這樣做,在python (我的問題是不是一個Python的問題,所以Qt的利弊請幫助)..我知道,我在什麼地方錯了處理餅乾和所有,所以請幫助。請提供可能的解決方案。
在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
這一切都是自動的蟒蛇做。 下面的代碼是沒有直接關係,但我這個編碼,以便在POST重定向。該代碼是very similar to the link I used to make it..
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;
}
這是我第一次嘗試使用QT製作應用程序,因此如果它不符合標準,請耐心等待。 – 2010-12-22 13:10:31