使用Qt设置http获取请求参数 [英] Setting http get request parameters using Qt

查看:22
本文介绍了使用Qt设置http获取请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Qt 中开发一个基本应用程序,该应用程序使用 REST API 从 Parse.com 检索数据.我浏览了一些类参考和 cURL 手册,但仍然不清楚您如何设置请求参数.例如,我想对用户进行身份验证.这是 Parse 提供的 curl 示例:

I'm developing a basic application in Qt that retrieves data from Parse.com using the REST API. I went through some class references and the cURL manual but it's still not clear how you set the request parameters. For example, I'd like to authenticate a user. Here's the curl example provided by Parse:

curl -X GET 
-H "X-Parse-Application-Id: myappid" 
-H "X-Parse-REST-API-Key: myapikey" 
-G 
--data-urlencode 'username=test' 
--data-urlencode 'password=test' 
https://api.parse.com/1/login

我像这样设置了网址和标题

I set the url and the headers like this

QUrl url("https://api.parse.com/1/login");
QNetworkRequest request(url);

request.setRawHeader("X-Parse-Application-Id", "myappid");
request.setRawHeader("X-Parse-REST-API-Key", "myapikey");

nam->get(request);

当没有参数时它工作得很好,但是我应该使用什么来实现与 curl 与 --data-urlencode 开关相同的效果?

which worked fine when there were no parameters, but what should I use to achieve the same as curl does with the --data-urlencode switch?

感谢您的时间

推荐答案

不幸的是,QUrl::addQueryItem() 在 qt5 中已被弃用,但从那里开始我发现了 QUrlQuery 类,它有一个 addQueryItem() 方法并且可以生成一个查询QUrl 的 setQuery() 方法可接受的字符串,因此它现在看起来像这样并且工作正常:

Unfortunately, QUrl::addQueryItem() is deprecated in qt5 but starting from there I found the QUrlQuery class which has an addQueryItem() method and can produce a query string that is acceptable for QUrl's setQuery() method so it now looks like this and works fine:

QUrl url("https://api.parse.com/1/login");
QUrlQuery query;

query.addQueryItem("username", "test");
query.addQueryItem("password", "test");

url.setQuery(query.query());

QNetworkRequest request(url);

request.setRawHeader("X-Parse-Application-Id", "myappid");
request.setRawHeader("X-Parse-REST-API-Key", "myapikey");

nam->get(request);

感谢克里斯的提示.

这篇关于使用Qt设置http获取请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆