내 qhttp get () 호출은 Windows에서 작동하지 않지만 Linux에서 수행됩니다.

StackOverflow https://stackoverflow.com/questions/803088

  •  03-07-2019
  •  | 
  •  

문제

QHTTP를 사용하여 웹 페이지를 얻는 프로그램을 작성했습니다. 이것은 Linux에서 잘 작동하지만 Windows Box (Vista)에서는 작동하지 않습니다. QHTTP 완료 신호는 수신되지 않은 것으로 보입니다.

관련 코드는 다음과 같습니다.

    Window::Window()
{
    http = new QHttp(this);
    connect(http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
url = new QUrl("http://something.com/status.xml");
http->setHost(url->host(), url->port() != -1 ? url->port() : 80);
    if (!url->userName().isEmpty()) http->setUser(url->userName(), url->password());
}

void Window::retrievePage()
{ 
byteArray = new QByteArray;
result = new QBuffer(byteArray);
result->open(QIODevice::WriteOnly);

    httpRequestAborted = false;
    httpGetId = http->get(url->path(), result);
 }

 void Window::httpDone(bool error)
 {
     //Never gets here!
 }

모든 도움이 승인됩니다.

매트

도움이 되었습니까?

해결책

This should not happen at all, i.e. QHttp works reliably both on Windows and Unix.

My advice is to check whether the serves gives proper response. This can be done e.g. by verifying that data transfer is fine. You can trace the status from QHttp's signal, e.g. dataReadProgress, requestStarted, requestFinished, and other related signals.

On the other hand, instead of using old QHttp, why not using the recommended QNetworkAccessManager instead? To get your feet wet quickly, check an example I posted to Qt Labs some time ago: image viewer with remote URL drag-and-drop support. It uses the said QNetworkAccessManager to grab the image from the dropped URL. Check the source-code, it is only 150 lines.

다른 팁

Rewritten as suggested by Ariya to use QNetworkAccessManager and looking at this example

This now works on Windows and Linux.

Window::Window()
{
   connect(&manager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(retrieveData(QNetworkReply*)));
}

void Window::retrieveMessage()
{
    manager.get(QNetworkRequest(QUrl("http://...")));
}

void Window::retrieveData(QNetworkReply *reply)
{
    QVariant statusCodeV = 
    reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    // "200 OK" received?
    if (statusCodeV.toInt()==200)
    {
        QByteArray bytes = reply->readAll();  // bytes
    }

    reply->deleteLater();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top