Results 1 to 4 of 4

Thread: Connecting to an FTP server and calling web service files?

  1. #1
    Join Date
    Jul 2020
    Location
    Michigan
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default 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
    Qt Code:
    1. //connection//
    2. QNetworkAccessManager *manager = new QNetworkAccessManager();
    3. QUrl url2("ftp://ftp.[site]");
    4. url2.setPort(#);
    5. url2.setPassword("[pass]");
    6. url2.setUserName("[user]");
    7. QNetworkRequest req(url2);
    8. QNetworkReply *reply = manager->get(req);
    9. //end connection//
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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:
    Qt Code:
    1. QUrl url2("ftp://ftp.example.com/path/to/file.txt");
    To copy to clipboard, switch view to plain text mode 
    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.

  3. #3
    Join Date
    Jul 2020
    Location
    Michigan
    Posts
    8
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default 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?

    Qt Code:
    1. QNetworkAccessManager netMan;
    2. QNetworkReply* const repl = netMan.get(QNetworkRequest(QUrl::fromUserInput("ftp url direct to file")));
    3. QObject::connect(repl,&QNetworkReply::readyRead,[repl]()->void{qDebug().noquote() << repl->readAll();});
    4. QObject::connect(repl,QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[repl]()->void{qDebug() << "Error: " + repl->errorString();});
    5. QObject::connect(repl,&QNetworkReply::finished,repl,&QNetworkReply::deleteLater);
    6. QObject::connect(&netMan,&QNetworkAccessManager::authenticationRequired,repl,[repl](QNetworkReply *reply, QAuthenticator *authenticator)->void{
    7. if(reply!=repl)
    8. return;
    9. authenticator->setUser(" ");
    10. authenticator->setPassword(" ");
    11. });
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Connecting to an FTP server and calling web service files?

    Quote Originally Posted by luckachi View Post
    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
    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...).

Similar Threads

  1. Connecting to MySQL Server using SSL (AWS RDS)
    By sathhin in forum Qt Programming
    Replies: 0
    Last Post: 8th June 2016, 14:15
  2. Problems connecting to SQL Server 2005 from Windows 7
    By redBeard in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2012, 00:18
  3. QSslSocket - problem with connecting to the server
    By kremuwa in forum Qt Programming
    Replies: 9
    Last Post: 26th August 2010, 15:40
  4. connecting 2 dylib files
    By munna in forum Installation and Deployment
    Replies: 1
    Last Post: 22nd November 2006, 13:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.