使用 Qt 进行基本身份验证 (QNetworkAccessManager)

发布于 2024-08-06 10:38:31 字数 427 浏览 13 评论 0原文

我试图从我的 Qt 应用程序执行 Twitter 的基本身份验证。我使用 QNetworkAccessManager。但我在这方面找不到任何帮助。

但我发现了一个名为 qsoapmanager 的程序,它通过标头以 base64 形式传递凭据。也许我可以通过在 QNetowrkRequest 中设置标头来使用 QNAM 来做到这一点。但我没能找到方法。

在 qsoapman 源代码中,标头设置如下:

QHttpRequestHeader header;

header.setValue( "Authorization", QString( "Basic " ).append( auth.data() ) );

Can I do just that with QNAM/QNReq or is there a better way?

I was trying to perform basic authentication for Twitter from my Qt app. I use QNetworkAccessManager. But I couldn't find any help on this.

But I found a program called qsoapmanager which passes credentials in base64 through the header. Maybe I can do this with QNAM by setting header in QNetowrkRequest. But I failed to find a way.

In qsoapman source, header is set like this:

QHttpRequestHeader header;

header.setValue( "Authorization", QString( "Basic " ).append( auth.data() ) );

Can I do just that with QNAM/QNReq or is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-08-13 10:38:32

The recommended way is to connect to the authenticationRequired signal and set the credentials from there.

深白境迁sunset 2024-08-13 10:38:32

只是正常使用 qNetworkAccessManager 但添加

setRawHeader("Authorization", headerData.toLocal8Bit());

到您的请求。

示例:

//authentication

QString concatenated = "admin:admin"; //username:password

QByteArray data = concatenated.toLocal8Bit().toBase64();

QString headerData = "Basic " + data;

QNetworkRequest request=QNetworkRequest(QUrl("http://192.168.1.10/getinfo"));

request.setRawHeader("Authorization", headerData.toLocal8Bit());

networkAccessManager->get(request);

`

Just using qNetworkAccessManager normally but add

setRawHeader("Authorization", headerData.toLocal8Bit());

to your request.

Example:

//authentication

QString concatenated = "admin:admin"; //username:password

QByteArray data = concatenated.toLocal8Bit().toBase64();

QString headerData = "Basic " + data;

QNetworkRequest request=QNetworkRequest(QUrl("http://192.168.1.10/getinfo"));

request.setRawHeader("Authorization", headerData.toLocal8Bit());

networkAccessManager->get(request);

`

顾北清歌寒 2024-08-13 10:38:31

但是,如果您只想通过设置标头值来完成此操作,请按以下步骤操作:

// HTTP Basic authentication header value: base64(username:password)
QString concatenated = username + ":" + password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader("Authorization", headerData.toLocal8Bit());

But if you want to do it by just setting the header value, here's how you can do that:

// HTTP Basic authentication header value: base64(username:password)
QString concatenated = username + ":" + password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader("Authorization", headerData.toLocal8Bit());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文