| 78 | #if defined(QHTTP_HAS_CLIENT) |
| 79 | |
| 80 | void runClient(QString url) { |
| 81 | using namespace qhttp::client; |
| 82 | // ensure there the url has http:// |
| 83 | if ( !url.startsWith("http://") && !url.startsWith("https://") ) |
| 84 | url.prepend("http://"); |
| 85 | |
| 86 | QHttpClient client(qApp); |
| 87 | |
| 88 | bool success = client.request(qhttp::EHTTP_GET, url, [](QHttpResponse* res) { |
| 89 | // response handler, called when the HTTP headers of the response are ready |
| 90 | res->collectData(); |
| 91 | // called when all data in HTTP response have been read. |
| 92 | res->onEnd([res]() { |
| 93 | // print the XML body of the response |
| 94 | qDebug("\nreceived %d bytes of http body:\n%s\n", |
| 95 | res->collectedData().size(), |
| 96 | res->collectedData().constData() |
| 97 | ); |
| 98 | |
| 99 | qApp->quit(); |
| 100 | }); |
| 101 | |
| 102 | // just for fun! print headers: |
| 103 | qDebug("\n[Headers:]"); |
| 104 | res->headers().forEach([](auto cit) { |
| 105 | qDebug("%s : %s", cit.key().constData(), cit.value().constData()); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | if ( !success ) { |
| 110 | qDebug("failed: can not send a request to %s\n", qPrintable(url)); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | qApp->exec(); |
| 115 | } |
| 116 | |
| 117 | void runWeatherClient(const QString& cityName) { |
| 118 | using namespace qhttp::client; |
no test coverage detected