问题
I'm trying to use this http://www.rcsb.org/pdb/software/rest.do REST services with Qt. I did some get requests with no problem, but when I try to do a post request to its advanced search(which is an XML web service), I get no response.
This is the post request I'm trying:
<orgPdbQuery>
<queryType>org.pdb.query.simple.StructureIdQuery</queryType>
<description>Simple query for a list of PDB IDs (1 IDs) : 3I5F</description>
<structureIdList>3I5F</structureIdList>
</orgPdbQuery>
And this is my code for the request:
void WindowWrapper::postRequest()
{
QNetworkRequest request;
QUrl res = QUrl(request_url_);
QUrl query;
query.addQueryItem("queryType","org.pdb.query.simple.StructureIdQuery");
query.addQueryItem("structureIdList","3I5F");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
request.setUrl(res);
QObject::connect(network_, SIGNAL(finished(QNetworkReply*)),
this, SLOT(slotRequestFinished(QNetworkReply*)));
network_->post(request, query.encodedQuery());
}
void WindowWrapper::slotRequestFinished(QNetworkReply* reply)
{
if(reply->error() > 0)
{
qDebug() << reply->errorString();
}
else
{
QByteArray data = reply->readAll();
qDebug() << "Request successful!";
qDebug() << data;
}
}
And the method call:
wrapper_->set_request_url("http://www.rcsb.org/pdb/rest/search/");
wrapper_->postRequest();
And on my debug output I get this:
Request successful!
""
Edit:
I also tried this for the request, but I still got no response:
void WindowWrapper::postRequest()
{
QNetworkRequest request;
request.setRawHeader("Content-Type", "text/xml;charset=UTF-8");
request.setUrl(QUrl(request_url_));
QString query =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<orgPdbQuery>"
"<queryType>org.pdb.query.simple.StructureIdQuery</queryType>"
"<description>Simple query for a list of PDB IDs (1 IDs) : 3I5F</description>"
"<structureIdList>3I5F</structureIdList>"
"</orgPdbQuery>";
QObject::connect(network_, SIGNAL(finished(QNetworkReply*)),
this, SLOT(slotRequestFinished(QNetworkReply*)));
network_->post(request, query.toUtf8());
}
Anyone knows what am I doing wrong? Please...
回答1:
The link you gave says the query data has to be encoded in XML.
And I don't know why, but the service seems to only allow application/x-www-form-urlencoded as Content-Type, all other types trigger a redirection to the 'rest.do' page.
回答2:
I dont think that you are sending data in right way. You are trying to create request by following code, which will not send xml request to server.
QUrl query;
query.addQueryItem("queryType","org.pdb.query.simple.StructureIdQuery");
query.addQueryItem("structureIdList","3I5F");
you need to do something like following to send xml data to server.
QNetworkRequest request(QUrl("https://graph.facebook.com/me/feed");
mCurrentRequest = mNetManager.post(request,postData.toAscii());
connect(mCurrentRequest,SIGNAL(finished()),this,SLOT(messageResponse()));
Here in above code postData should be your xml request. you can refer this link for more info.
来源:https://stackoverflow.com/questions/9847433/qt-web-service-post-query