Results 1 to 2 of 2

Thread: Trying to ping the server via QSsl

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Trying to ping the server via QSsl

    We are trying to connect via Qssl::TlsV1 to the server.But unable to ping.Following is the source code where we are trying to add TlsV1.Please provide the solution on this matter.


    Qt Code:
    1. #include "httprequest.h"
    2. #include<QDebug>
    3. #include<QSslConfiguration>
    4. #include<QSsl>
    5. #include "terminalsetting.h"
    6.  
    7. const QString HttpRequest::CONTENT_TYPE_APPLICATION_OCTET_STREAM="application/octet-stream"; //application/octet-stream
    8. const QString HttpRequest::CONTENT_TYPE_APPLICATION_TEXT_PLAIN="text/plain";
    9. const QString HttpRequest::CONTENT_TYPE_IMAGE_PNG="image/png";
    10. const QString HttpRequest::CONTENT_TYPE_TAR="application/x-tar";
    11. //const QString HttpRequest::CERTIFICATE_PATH="/mnt/jffs2/blb/cert/cert.crt";
    12.  
    13.  
    14.  
    15.  
    16. HttpRequest::HttpRequest(QObject *parent = 0) : QObject(parent)
    17. {
    18. //_http = new QNetworkAccessManager(this);
    19. }
    20.  
    21. HttpRequest::~HttpRequest()
    22. {
    23. // delete _http;
    24. }
    25.  
    26. void HttpRequest::initCertificates(){
    27. qDebug()<<" == initCertificates == ";
    28.  
    29. QList<QSslCertificate> CaCert= QSslCertificate::fromPath(TerminalSetting::getSSLCertificateFolder().append("/cert.crt"));
    30. QSslSocket::setDefaultCaCertificates(CaCert);
    31.  
    32. }
    33.  
    34. RestResponse HttpRequest::get(const QString &requestUrl,const long timeoutInSecond)
    35. {
    36. QEventLoop eventLoop;
    37. QNetworkAccessManager mgr;
    38.  
    39. QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    40. long TIME_OUT_IN_SECONDS = timeoutInSecond;
    41. if(timeoutInSecond <= 0)
    42. TIME_OUT_IN_SECONDS = 3600;
    43.  
    44. QNetworkRequest request;
    45. request.setUrl(QUrl(requestUrl));
    46.  
    47. initCertificates();
    48.  
    49. qDebug()<<" == inside HttpRequest get == "<<requestUrl;
    50. QSslConfiguration config = request.sslConfiguration();
    51. config.setProtocol(QSsl::TlsV1);
    52. request.setSslConfiguration(config);
    53.  
    54. QTimer timer;
    55. timer.setSingleShot(true);
    56. connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
    57.  
    58. QNetworkReply *reply = mgr.get(request);
    59. timer.start(TIME_OUT_IN_SECONDS * 1000);
    60. eventLoop.exec(); // blocks stack until "finished()" has been called
    61. if(timer.isActive()){
    62. qDebug()<<"Timer running...";
    63. timer.stop();
    64. }
    65. //time out
    66. else{
    67. qDebug()<<" TIME OUT";
    68. return RestResponse::nullResponse();
    69. }
    70.  
    71. int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    72. if(status == 0)
    73. return RestResponse::nullResponse();
    74.  
    75. if (reply->error() == QNetworkReply::NoError)
    76. res = reply->readAll();
    77. else
    78. res.append(reply->errorString());
    79. delete reply;
    80.  
    81.  
    82. return RestResponse(status,res);
    83. }
    84.  
    85. RestResponse HttpRequest::post(const QString &url,const QList<FormParam> &data,const long timeoutInSecond)
    86. {
    87. QEventLoop eventLoop;
    88. QNetworkAccessManager mgr;
    89. QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    90.  
    91. QByteArray postData;
    92.  
    93. foreach (FormParam param, data) {
    94. postData.append("--"+QString(POST_MSG_BOUNDARY)+"\r\n");
    95. postData.append("Content-Disposition: form-data; name=""\""+param.getName()+"\"");
    96. postData.append("\r\n");
    97. postData.append("Content-Type: "+param.getContentType()+"\r\n\r\n");
    98. qDebug()<<"Content data " <<param.getValue();
    99. postData.append(param.getValue());
    100. postData.append("\r\n");
    101. }
    102. postData.append("--"+QString(POST_MSG_BOUNDARY)+"\r\n");
    103.  
    104.  
    105. QUrl serviceUrl(url);
    106. // the HTTP request
    107. QNetworkRequest req(serviceUrl);
    108. req.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    109. req.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + QString(POST_MSG_BOUNDARY)).toAscii());
    110. //SSL Configuration
    111. initCertificates();
    112. qDebug()<<" == inside httpRequest post == ";
    113. QSslConfiguration config = req.sslConfiguration();
    114. config.setProtocol(QSsl::TlsV1);
    115. req.setSslConfiguration(config);
    116. QTimer timer;
    117. long TIME_OUT_IN_SECONDS = timeoutInSecond;
    118. if(timeoutInSecond <= 0)
    119. TIME_OUT_IN_SECONDS = 3600;
    120.  
    121. timer.setSingleShot(true);
    122. connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
    123.  
    124. QNetworkReply *reply = mgr.post(req,postData);
    125. timer.start(TIME_OUT_IN_SECONDS * 1000);
    126. eventLoop.exec(); // blocks stack until "finished()" has been called
    127. if(timer.isActive()){
    128. qDebug()<<"Timer running...";
    129. timer.stop();
    130. }
    131. //time out
    132. else{
    133. qDebug()<<" TIME OUT";
    134. return RestResponse::nullResponse();
    135. }
    136.  
    137.  
    138. int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    139. qDebug()<<" int status ::: "<<status;
    140. if(status == 0)
    141. return RestResponse::nullResponse();
    142. if (reply->error() == QNetworkReply::NoError)
    143. res = reply->readAll();
    144. else
    145. res.append(reply->errorString());
    146. delete reply;
    147. return RestResponse(status,res);
    148. }
    149.  
    150. RestResponse HttpRequest::postJSON(const QString &url, const QMap<QString, QString> &data,const long timeoutInSecond)
    151. {
    152. QEventLoop eventLoop;
    153. QNetworkAccessManager mgr;
    154. QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    155.  
    156. QByteArray postData ="{";
    157. int index = 0;
    158.  
    159. foreach (QString key, data.keys()) {
    160. if(index > 0 && index != (data.size()))
    161. postData.append(",");
    162. postData.append("\"");
    163. postData.append(key);
    164. postData.append("\"");
    165. postData.append(":");
    166. postData.append("\"");
    167. postData.append(data.value(key));
    168. postData.append("\"");
    169.  
    170. index++;
    171. }
    172. postData.append("}");
    173. QByteArray postDataSize = QByteArray::number(postData.size());
    174. QUrl serviceUrl(url);
    175. // the HTTP request
    176. QNetworkRequest req(serviceUrl);
    177. //SSL Configuration
    178. initCertificates();
    179.  
    180. qDebug()<<" == inside httpRequest postJSON == ";
    181. QSslConfiguration config = req.sslConfiguration();
    182. config.setProtocol(QSsl::TlsV1);
    183. req.setSslConfiguration(config);
    184.  
    185. QTimer timer;
    186. long TIME_OUT_IN_SECONDS = timeoutInSecond;
    187. if(timeoutInSecond <= 0)
    188. TIME_OUT_IN_SECONDS = 3600;
    189. timer.setSingleShot(true);
    190. connect(&timer,SIGNAL(timeout()),&eventLoop,SLOT(quit()));
    191.  
    192. req.setRawHeader("User-Agent", "CC v0.1");
    193. req.setRawHeader("X-Custom-User-Agent", "CC Client");
    194. req.setRawHeader("Content-Type", "application/json");
    195. req.setRawHeader("Content-Length", postDataSize);
    196. req.setRawHeader("Accept", "application/json"); //Accept:application/json
    197.  
    198. qDebug()<<postData;
    199.  
    200. QNetworkReply *reply = mgr.post(req,postData);
    201. timer.start(TIME_OUT_IN_SECONDS * 1000);
    202. eventLoop.exec(); // blocks stack until "finished()" has been called
    203. if(timer.isActive()){
    204. qDebug()<<"Timer running...";
    205. timer.stop();
    206. }
    207. //time out
    208. else{
    209. qDebug()<<" TIME OUT";
    210. return RestResponse::nullResponse();
    211. }
    212. int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    213. if(status == 0)
    214. return RestResponse::nullResponse();
    215.  
    216. if (reply->error() == QNetworkReply::NoError)
    217. res = reply->readAll();
    218. else
    219. res.append(reply->errorString());
    220. delete reply;
    221. return RestResponse(status,res);
    222.  
    223. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2015
    Posts
    14
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Trying to ping the server via QSsl

    Here is the shortest one:

    Step: 1
    // Create QProcess object
    // m_sHostName = "8.8.8.8" or use "www.google.com"
    proc = new QProcess();
    proc->start("ping", QStringList() << "-c" << "1" << QString(m_sHostName));

    // Show output
    connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(rightMessage()));
    connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage()));


    Step 2:
    if(proc->waitForFinished(1000) == true)
    {
    proc->terminate();
    sleep(1);
    QMessageBox msgBox1;
    msgBox1.setText("Ping tamamlandı... ");
    msgBox1.exec();
    }

    Step 3:

    // Show right message
    void DialogWIFI::rightMessage()
    {
    QByteArray strdata = proc->readAllStandardOutput();
    strdata = strdata.simplified();
    strdata = strdata.trimmed();

    ui->TextEditResult->setTextColor(Qt::white);
    ui->TextEditResult->append(strdata);
    ui->TextEditResult->append("<br>");
    QCoreApplication:rocessEvents();
    gIsPinged = true;
    }

    Kind Regards

Similar Threads

  1. Https server - logging by QSsl
    By Trok in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2010, 00:22
  2. Qssl
    By tpthyd in forum Qt Programming
    Replies: 16
    Last Post: 15th June 2009, 10:53
  3. qssl
    By jsmith in forum Qt Programming
    Replies: 29
    Last Post: 7th April 2009, 23:21
  4. Replies: 1
    Last Post: 13th November 2008, 11:46
  5. How to ping a server ?
    By Nyphel in forum Newbie
    Replies: 2
    Last Post: 23rd April 2007, 12:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.