Connecting to an FTP server and calling web service files?
Hi everyone, I am a newbie when it comes to Qt - I've only been developing for about a month or so and I do not have a developer background. Be gentle! :)
I am in the process of creating a new version of an app we have using Qt Creator and I've hit a road block. I am in need of some help understanding how to connect to an ftp server with credentials and automatically download a few files as soon as the app opens. I've looked at a few examples but nothing seems to work / make sense to me and all I have so far is below. I am slowly understandings slots and signals but I am still not able to wrap my brain around it entirely.
main.cpp
Code:
//connection//
QNetworkAccessManager *manager = new QNetworkAccessManager();
QUrl url2
("ftp://ftp.[site]");
url2.setPort(#);
url2.setPassword("[pass]");
url2.setUserName("[user]");
QNetworkRequest req(url2);
QNetworkReply *reply = manager->get(req);
//end connection//
Also, is there a way to call a web service (php file / SOAP) via a button click? A co-worker of mine mentioned calling it in javascript and then calling the javascript onClick but I haven't found and good examples of this.
Thank you for your time!
Re: Connecting to an FTP server and calling web service files?
If you know the names of the files you need to fetch then it should be as simple as providing a complete URL using the FTP: scheme:
Code:
QUrl url2
("ftp://ftp.example.com/path/to/file.txt");
then your get() call will do the right thing. When data starts returning you will get readyRead() signals from the QNetworkReply object to handle in a slot. When the connection is made you may also get a metadataChanged() signal with some details about the file.
If you need to navigate the site to find files of unknown name then it becomes more complicated.
Regarding the SOAP request: yes it is possible for an HTTP endpoint. You will need to construct the SOAP payload yourself, ensure the correct Content-Type header (maybe others, e.g. SOAPAction) for the request then post() it. The response come back just like a web server, just with a SOAP structure and Content-Type.
Re: Connecting to an FTP server and calling web service files?
Hi Chris, thank you for your response - I appreciate it. I am working with a console application versus a GUI app and the slots / signals still confuse me a bit. I re-wrote my ftp a bit but I am not sure all of this is necessary compared to my initial post. I assume the way to do the signal and slots is the QObject::connect lines? For the web service PHP, I assume this should go into the main.cpp file versus placing it on the actual page even though I want it to send the information to the file on a button click?
Code:
QNetworkAccessManager netMan;
QNetworkReply
* const repl
= netMan.
get(QNetworkRequest
(QUrl::fromUserInput("ftp url direct to file")));
QObject::connect(repl,
&QNetworkReply
::readyRead,
[repl
]()->void
{qDebug
().
noquote() << repl
->readAll
();
});
QObject::connect(repl,QOverload<QNetworkReply
::NetworkError>
::of(&QNetworkReply
::error),
[repl
]()->void
{qDebug
() <<
"Error: " + repl
->errorString
();
});
QObject::connect(repl,
&QNetworkReply
::finished,repl,
&QNetworkReply
::deleteLater);
QObject::connect(&netMan,
&QNetworkAccessManager
::authenticationRequired,repl,
[repl
](QNetworkReply
*reply, QAuthenticator
*authenticator
)->void
{ if(reply!=repl)
return;
authenticator->setUser(" ");
authenticator->setPassword(" ");
});
Re: Connecting to an FTP server and calling web service files?
Quote:
Originally Posted by
luckachi
Hi Chris, thank you for your response - I appreciate it. I am working with a console application versus a GUI app and the slots / signals still confuse me a bit. I re-wrote my ftp a bit but I am not sure all of this is necessary compared to my initial post. I assume the way to do the signal and slots is the QObject::connect lines?
The connect() calls are part of the signal/slot mechanism. The other part is a running event loop that delivers emitted signals to listening slots. You need these components on the client side regardless of GUI/console choice. On a quick inspection your code snippet looks OK for the first part. For a beginner it may be easier to follow if you split the slots into standalone functions because you can then trace execution more obviously with your debugger. You need to be careful about the scope of the netMan object: it needs to exist to process requests/responses.
I assume you have read the docs
Quote:
For the web service PHP, I assume this should go into the main.cpp file versus placing it on the actual page even though I want it to send the information to the file on a button click?
All the client code belongs in the client application. The server providing your service is just another place you communicate with, and it does not matter how that server implements the service (C++, PHP, Java, or COBOL :)). If you are using a mechanism like SOAP then you have an agreed way to express requests to the server and receive responses from it. The SOAP requests and responses are the payload of (usually) an HTTP request and response. You can see an example pair for a simple service: https://www.dataaccess.com/webservic...=NumberToWords
I am not sure where a button click comes from in a console application. However, the process of constructing and sending a SOAP request is the same regardless of the event (button click, timer, whatever...).