WEB-API无法获取QT Post方法发送的body数据

前端使用QT,构建url,发送get和post方法,QT的post代码如下:

QNetworkAccessManager *m_pHttpMgr = new QNetworkAccessManager(this);
QNetworkRequest requestInfo;
QString url = "http://自己的url";
QUrl strUrl = url;
requestInfo.setUrl(strUrl);

requestInfo.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
requestInfo.setRawHeader("Content-Type", "charset='utf-8'");
    
QByteArray bodyData;
bodyData.append("name=john&");
bodyData.append("password=123456");

QNetworkReply *reply = m_pHttpMgr->post(requestInfo, bodyData);
QEventLoop eventLoop;
connect(m_pHttpMgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
eventLoop.exec();

if (reply->error() == QNetworkReply::NoError)
{
	QByteArray responseByte = reply->readAll();
    //成功
}
else
{
	//失败
}
reply->deleteLater();

后台C#  WEB-API代

As an AI language model, I do not have the capability to provide a demo. However, I can explain how to create a simple Qt HTTP Web API demo. First, you need to include the necessary headers: ``` #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> ``` Then, you can create a `QNetworkAccessManager` object to send HTTP requests: ``` QNetworkAccessManager *manager = new QNetworkAccessManager(this); ``` You can then use the `get()` method to send a GET request to a Web API: ``` QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("https://jsonplaceholder.typicode.com/posts"))); connect(reply, &QNetworkReply::finished, [=]() { if (reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); // process the data received from the Web API } else { // handle the error } reply->deleteLater(); }); ``` In the above example, the `QByteArray` `data` contains the response data received from the Web API. You can then process this data as needed. You can also use the `post()` method to send a POST request with data: ``` QNetworkRequest request(QUrl("https://jsonplaceholder.typicode.com/posts")); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); QJsonObject postData; postData["title"] = "foo"; postData["body"] = "bar"; postData["userId"] = 1; QJsonDocument doc(postData); QByteArray postDataBytes = doc.toJson(); QNetworkReply *reply = manager->post(request, postDataBytes); connect(reply, &QNetworkReply::finished, [=]() { if (reply->error() == QNetworkReply::NoError) { QByteArray data = reply->readAll(); // process the data received from the Web API } else { // handle the error } reply->deleteLater(); }); ``` In the above example, a JSON object is created and serialized to a `QByteArray`. This data is then sent in the body of the POST request. This is just a basic example of how to use Qt to interact with a Web API. There are many other features and options available in Qt that can be used to customize and optimize the communication with the Web API.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值