2010-12-22 8 views
6

मैं इस पूरे दिन कोई सफलता नहीं कर रहा हूं। कृपया इस मुद्दे को हल करने में मदद करें। गुगलिंग पर मैंने पाया कि कई उपयोगकर्ताओं को यह समस्या थी लेकिन कहीं भी मुझे कोई समाधान नहीं मिला।क्यूटी HTTP पोस्ट समस्या जब सर्वर को कुकीज़ की आवश्यकता होती है

मैं (ताकि क्यूटी पेशेवरों मदद कृपया, मेरा प्रश्न एक अजगर सवाल नहीं है) मैं पहले से ही कर रही है कि अजगर में की कोशिश की है क्यूटी सी ++ & में HTTP पोस्ट करने के लिए कोशिश कर रहा हूँ .. मुझे पता है, मैं कहीं गलत हूँ कुकीज़ और सभी को संभालना, तो कृपया मदद करें। कृपया संभावित समाधान प्रदान करें।

पायथन में, कोड साफ़ और सरल है। मैंने त्रुटि को संभालने और इसे आसान बनाने के लिए सभी अतिरिक्त चीजें छीन ली हैं।

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) 

क्यूटी समतुल्य मैंने बनाया: -

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); 
} 

अब मुद्दा है कि मैं क्यूटी में भी ऐसा ही करने में सक्षम नहीं हूँ। समस्याओं का सामना करना पड़ रहा हूँ:

  1. हैंडलिंग कुकीज़
  2. रीडायरेक्ट हैंडलिंग (HTTP 302)
  3. कुकीज़ बनाए रखना भविष्य पोस्ट

यह सब अजगर में स्वतः किया जाता है बनाने के लिए। नीचे, कोड सीधे संबंधित नहीं है, लेकिन मैं इस कोडित पोस्ट में रीडायरेक्ट .. कोड 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; 
} 
+0

यह मेरा क्यूटी का उपयोग करके ऐप्लिकेशन बनाने के लिए है, तो मेरे साथ सहन कृपया पहला प्रयास है अगर इसकी सही रूप में मानकों के अनुसार नहीं। –

उत्तर

7

मैं इस का उपयोग एक कुकी प्राप्त करने के लिए:

SomeDialog::SomeDialog(QWidget *parent) 
    : QDialog(parent) 
     , urlSearch("www.someotherurlyoumightneed.com") 
    , urlCookie("www.urltogetcookie.from") 
{ 
    ui.setupUi(this); 

    //manager is a QNetworkAccessManager 
    manager.setCookieJar(new QNetworkCookieJar); 
    connect(&manager, SIGNAL(finished(QNetworkReply*)), 
     SLOT(slotReplyFinished(QNetworkReply*))); 

    //this is a QNetworkRequest 
    //here i tell how the post methods are encoded 
    searchReq.setUrl(urlSearch); 
    searchReq.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); 

    //get cookie 
    manager.get(QNetworkRequest(urlCookie)); 
    lblStatus->setText("Getting cookie"); 
} 

void SomeDialog::slotReplyFinished(QNetworkReply* reply){ 
    reply->deleteLater(); 

    if(reply->error() != QNetworkReply::NoError){ 
     QMessageBox::warning(this,QString(), tr("Error while downloading information!\n%1").arg(reply->errorString())); 

     return; 
    } 

    //Here i check if there is a cookie for me in the reply and extract it 
    QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie>>(reply->header(QNetworkRequest::SetCookieHeader)); 
    if(cookies.count() != 0){ 
     //you must tell which cookie goes with which url 
     manager.cookieJar()->setCookiesFromUrl(cookies, urlSearch); 
    } 

    //here you can check for the 302 or whatever other header i need 
    QVariant newLoc = reply->header(QNetworkRequest::LocationHeader); 
    if(newLoc.isValid()){ 
     //if it IS a reloc header, get the url it points to 
     QUrl url(newLoc.toString()); 
     _req.setUrl(url); 
     _pendingReq.insert(_manager.get(_req)); 
     return; 
    } 

    //if you have multiple urls you are waiting for replys 
    //you can check which one this one belongs to with 
    if(reply->url() == urlSearch){ 
     //do something 
    } 
} 

void SomeDialog::slotSearch(){ 
    //Here we set the data needed for a post request 
    QList<QNetworkCookie> cookies = manager.cookieJar()->cookiesForUrl(urlSearch); 
    for(auto it = cookies.begin(); it != cookies.end(); ++it){ 
     searchReq.setHeader(QNetworkRequest::CookieHeader, QVariant::fromValue(*it)); 
    } 

    QUrl post; 
    post.addQueryItem("firstParameter", s); 
    post.addQueryItem("secondParameter", "O"); 
    QByteArray ba; 
    ba.remove(0,1); //must remove last & 

    searchReq.setUrl(urlSearch); 
    pendingReq.insert(manager.post(searchReq, ba)); 
} 

आशा इस मदद करता है।

0

मुझे लगता है कि कुकीज़ यूआरएल वे से प्राप्त कर रहे हैं से जुड़े हुए हैं है अनुमति देने के लिए। तो एक अलग यूआरएल के साथ आपके दूसरे पोस्ट में, पहले पोस्ट से कुकीज़ अनुरोध के साथ नहीं भेजी जाती हैं।

संबंधित मुद्दे