私はこの一日中、何の成功も見せていません。問題の解決にお役立てください。グーグルで私は多くのユーザーがこの問題を抱えていたが、私は解決策を見つけることができなかった。私はすでに(私の質問は、Pythonの問題ではないので、Qtのプロを助けてください)Pythonのに..私は知っている、私はどこか間違っていることをやって試してみましたが、私はQT C++ &にHTTPポストを実行しようとしていQT HTTPサーバーに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で同じことを行うことができないだということです。問題私が直面しています:クッキーを保持
- 取り扱いクッキーリダイレクトの取り扱い
- (HTTP 302)
- は将来のPOST
すべてこれはpythonで自動的に行われるようにします。以下 、コードを直接関連していないが、私はクッキーが彼らはから取得されたURLにリンクしていると思うのコードが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を使ってアプリを作るのはこれが私の最初の試みです。標準に準拠していない場合は、私にご負担ください。 –