Home > Back-end >  Qt HTTPS post TLS "Host the requires authentication."
Qt HTTPS post TLS "Host the requires authentication."

Time:04-03

Use the qt development a HTTPS login function, always prompt me "Host the requires authentication.", has been installed the relevant tutorial SSL plug-in installed,
Device access requirements are as follows:
Due to IP cannot be fixed bearing equipment, HTTPS server USES a self-signed certificate, so the client is configured HTTP TLS transport layer parameters, should be set to ignore the authentication server's certificate chain and the host name (i.e., to provide any server certificate and the certificate of any host name),
Methods:
At the time of visit has been designed for QueryPeer or VerifyNone, have no way to solve this problem, the HTML code is as follows, the great god gives directions,

Bool xx: : login (string host, string username, string password, int timeout)
{
//login address https://{host}/login
QUrl loginulr (host c_str ());
Loginulr. SetScheme (" HTTPS ");//set the HTTPS access
Loginulr. SetPath (" login ");//set the path

QNetworkRequest request;
Request. SetUrl (loginulr);
Request. SetRawHeader (" Accept ", "application/json");//the Accept: 'application/json'
Request. SetRawHeader (" the content-type ", "application/json");//'the content-type:' application/json '

QSslConfiguration config=QSslConfiguration: : defaultConfiguration ();
The config. SetPeerVerifyMode (QSslSocket: : QueryPeer);
The config. SetProtocol (QSsl: : AnyProtocol);
Request. SetSslConfiguration (config);

{//login information "username" : "demo", "password" : "demo"}
QJsonObject reqJson;
ReqJson. Insert (" username ", username. C_str ());
ReqJson. Insert (" password, "password. C_str ());
QNetworkReply * pReply=netmanager - & gt; Post (request, QJsonDocument (reqJson). ToJson () toStdString (). The c_str ());

//used to set the timeout
QTimer timer;
The timer. The setInterval (timeout);//set the timeout, the default is 10000 ms
The timer. SetSingleShot (true);//a single trigger

//set handle events
The QEventLoop loop;
The connect (& amp; The timer, & amp; QTimer: : timeout, & amp; Loop, & amp; QEventLoop: : quit);
The connect (pReply, & amp; QNetworkReply: : finished, & amp; Loop, & amp; QEventLoop: : quit);
The timer. The start ();
Loop. The exec ();//start the event loop

//handle the login event
If (timer. IsActive ()) {//process the response
The timer. Stop ();
If (pReply - & gt; Error ()!=QNetworkReply: : NoError) {
//return here every time they perform a "Host the requires authentication." mistakes, don't know is what reason
//error handling
LGlobalLogger: : Instance (). The log (LLogger: : MIError, "users: {%}, return data error, {%}," and the username, pReply - & gt; ErrorString (). ToStdString ());
LoginInfo. Online=false;//tag currently no login success
} else {
QVariant variant=pReply - & gt; The attribute (QNetworkRequest: : HttpStatusCodeAttribute);
Int nStatusCode=the variant, toInt ();
Cout & lt; <":" & lt; //if the successful login
If (nStatusCode==200) {
QByteArray bytes=pReply - & gt; ReadAll ();//read the returned json data
QJsonDocument jsonDocument=QJsonDocument: : fromJson (bytes. ToStdString (). The data ());
LGlobalLogger: : Instance (). The log (LLogger: : MIInfo,
JsonDocument. Object (). The value (" token "). The toString (). ToStdString ());//data to a log
If (jsonDocument isNull ()) {
LoginInfo. Online=false;//marked as no read success
LGlobalLogger: : Instance (). The log (LLogger: : MIInfo, "users: {%}, return data error, {%}," and the username, bytes. ToStdString ());
} else {
LoginInfo. Online=true;//tags for the login success
LoginInfo. Host=host;
LoginInfo. Username=username;
LoginInfo. Password=password;
LoginInfo. Updatetime=duration_cast & lt; Milliseconds> (system_clock: : now (). Time_since_epoch ()). The count ();//get the current timestamp, ms
LoginInfo. Token=jsonDocument. Object (). The value (" token "). The toString (). ToStdString ();
LoginInfo. DispName=jsonDocument. Object (). The value (" User "). ToObject (). The value (" displayName "). The toString (). ToStdString ();
LoginInfo. Id=jsonDocument. Object (). The value (" User "). ToObject (). The value (" id "). The toString (). ToStdString ();
LGlobalLogger: : Instance (). The log (LLogger: : MIInfo, "users: {%}, the login success," and the username, nStatusCode);
}
}
//login user and password error
If ((nStatusCode & gt;=400) & amp; & (nStatusCode & lt; {
=409))LoginInfo. Online=false;
LGlobalLogger: : Instance (). The log (LLogger: : MIError, "users: {%}, login fails, the error code {%}," and the username, nStatusCode);
}
//server internal error
If ((nStatusCode & gt;=500) & amp; & (nStatusCode & lt; {
=509))LoginInfo. Online=false;
LGlobalLogger: : Instance (). The log (LLogger: : MIError, "users: {%}, login fails, the error code {%}," and the username, nStatusCode);
}
PReply - & gt; DeleteLater ();//delete the data returned object
//connect the data processing of reestablishing
//connect (netmanager, & amp; QNetworkAccessManager: : finished, this, & amp; Llzdiaio: : replyFinished, Qt: : QueuedConnection);
}
} else {//processing timeout
Disconnect (pReply, & amp; QNetworkReply: : finished, & amp; Loop, & amp; QEventLoop: : quit);
PReply - & gt; Abort ();
PReply - & gt; DeleteLater ();
LoginInfo. Online=false;
LGlobalLogger: : Instance (). The log (LLogger: : MIError, "users: {%}, the server no response," and the username);
}
Return loginInfo. Online;
}

CodePudding user response:

The server is who? Asked him if he will be more clear, please you first try using tools such as postman interface can come again through the code
  • Related