在 Qt 中发送 SOAP 请求时的 ProtocolUnknownError
Posted
技术标签:
【中文标题】在 Qt 中发送 SOAP 请求时的 ProtocolUnknownError【英文标题】:ProtocolUnknownError upon sending SOAP request in Qt 【发布时间】:2017-11-06 06:08:25 【问题描述】:我正在做一个在 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();
引用的资源
http://www.qtcentre.org/threads/29856-sending-soap-request-using-QNetworkAccessManager-question
https://evileg.com/en/post/233/
Qt Web Service post query
Sending HTTP Header Info with Qt QNetworkAccessManager
Sending an HTTP request using QNetworkAccessManager
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
【问题讨论】:
在这里使用 Wireshark 可以准确了解 Qt 发送到服务器的内容。也就是说,SOAP 已经有将近二十年的历史了。现代代码应该使用 RESTful Web API。 【参考方案1】:调用manager.post(request, query.toUtf8());
将覆盖您的request.setRawHeader("POST", "/globalweather.asmx HTTP/1.1");
如果您想使用自定义标题等,请使用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"));
【讨论】:
以上是关于在 Qt 中发送 SOAP 请求时的 ProtocolUnknownError的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Flex 在 ISO-8859-1 中发送 SOAP 请求?