gpt4 book ai didi

c++ - 在 Qt 中发送 SOAP 请求时出现 ProtocolUnknownError

转载 作者:行者123 更新时间:2023-11-28 04:55:57 32 4
gpt4 key购买 nike

我正在做一个在 Qt 中发送 SOAP 请求的简单示例。尝试使用 QNetwork 资源并发送 SOAP 请求以获取某个国家/地区的城市。

http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry .

请帮忙;遗漏了什么。感谢您的任何指示/输入。

构建机器 - Linux Debian

Qt 版本 - 5.7

SSL - 1.0.2 可用。

qDebug() << "Support SSL:  " << QSslSocket::supportsSsl()
<< "\nLib Version Number: " << QSslSocket::sslLibraryVersionNumber()
<< "\nLib Version String: " << QSslSocket::sslLibraryVersionString()
<< "\nLib Build Version Number: " << QSslSocket::sslLibraryBuildVersionNumber()
<< "\nLib Build Version String: " << QSslSocket::sslLibraryBuildVersionString();

输出

Support SSL:   true 
Lib Version Number: 268443855
Lib Version String: "OpenSSL 1.0.2l 25 May 2017"
Lib Build Version Number: 268439647
Lib Build Version String: "OpenSSL 1.0.1e-fips 11 Feb 2013"

运行代码时出错

Network error:  QNetworkReply::NetworkError(ProtocolUnknownError)

代码

#include <QObject>
#include <QtNetwork>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QNetworkAccessManager manager;

QNetworkRequest request;

//Set SOAP headers

request.setRawHeader("POST", "/globalweather.asmx HTTP/1.1");
request.setRawHeader("Content-Type", "text/xml;charset=UTF-8");
request.setRawHeader("Content-Length", "1024");
request.setRawHeader("SOAPAction", "http://www.webserviceX.NET/GetCitiesByCountry");
//request.setRawHeader("Host", "www.webservicex.net");
request.setUrl(QUrl("www.webservicex.net"));


// Prepare SOAP request XML

QString query =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<GetCitiesByCountry xmlns=\"http://www.webserviceX.NET\">"
"<CountryName>Australia</CountryName>"
"</GetCitiesByCountry>"
"</soap:Body>"
"</soap:Envelope>";

QNetworkReply* reply = manager.post(request, query.toUtf8());

QEventLoop eventLoop;

QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();

if (reply->error() != QNetworkReply::NoError)
qDebug() << "Network error: " << reply->error();
else
qDebug() << reply->readAll();

return app.exec();
}

引用的资源

最佳答案

调用 manager.post(request, query.toUtf8()); 将覆盖你的 request.setRawHeader("POST", "/globalweather.asmx HTTP/1.1");

如果您想使用自定义 header 等,请使用 sendCustomRequest

或者在QNetworkRequest中设置正确的Uri并在 QNetworkAccessManager::post 中使用此请求方法。

    //request.setRawHeader("POST", "/globalweather.asmx HTTP/1.1");
request.setRawHeader("Content-Type", "text/xml;charset=UTF-8");
request.setRawHeader("Content-Length", "1024");
request.setRawHeader("SOAPAction", "http://www.webserviceX.NET/GetCitiesByCountry");
//request.setRawHeader("Host", "www.webservicex.net");
request.setUrl(QUrl("http://www.webservicex.net/globalweather.asmx"));

关于c++ - 在 Qt 中发送 SOAP 请求时出现 ProtocolUnknownError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47130736/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com