What is Curl
Curl is a command line tool and library. It is open source and run on various OS. Basically it is used to transferring data from a server to another server. It supports many types of Protocol like FTP, SFTP, POP3 SMB, SMTP, SMTPS, DICT, FILE, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3S, RTMP, RTSP, SCP. Curl also supports SSL certificate. In today's world, most of the equipment uses curl. Latest version of curl is 7.44.0 that released on August 12, 2015.
Download
You can download curl as per your Operating system compatible. It can be x86 or x64 as per your requirement. You can download it from here.
There are various protocols where we can use curl. But today I will explain you how to work with FTP and SFTP using curl.
Curl with FTP
FTP, means “File Transfer Protocol, in short we use FTP, is a standard network protocol that is used to transfer the data from one host to another host over a TCP based network like Internet.
You can use the following command with FTP.
Login usign curl on FTP
curl -P - --insecure "ftp://82.45.34.23:21/" --user "testuser:testpassword"
Upload using curl on FTP
curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/" --user "testuser:testpassword" -T "C:\test\testfile.xml" --ftp-create-dirs
Download using curl on FTP
curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/testfile.xml" --user "testuser:testpassword" -o "C:\test\testfile.xml" --ftp-create-dirs
Rename using curl on FTP
curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/" --user "testuser:testpassword" -Q "-RNFR /CurlPutTest/testfile.xml" -Q "-RNTO /CurlPutTest/testfile.xml.tmp" --ftp-create-dirs
Delete using curl on FTP
curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/testfile.xml" --user "testuser:testpassword" -Q "–DELE /CurlPutTest/testfile.xml" --ftp-create-dirs
Make directory using curl on FTP
Curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/test" --user "testuser:testpassword" -Q "-MKD /CurlPutTest/test" --ftp-create-dirs
Remove directory using curl on FTP
Curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/test" --user "testuser:testpassword" -Q "-RMD /CurlPutTest/test" --ftp-create-dirs
Curl with SFTP
SFTP, means “SSH File Transfer Protocol" or “Secure File Transfer Protocol”, in short we use SFTP, is a standard network protocol that is used to transfer the data from one host to another host over a secure connection.
You can use the following command with SFTP.
Login using curl on SFTP
curl -k "sftp://83.46.38.23:22/" --user "testuser:testpassword"
Upload using curl on SFTP
curl -k "sftp://83.46.38.23:22/CurlPutTest/" --user "testuser:testpassword" -T "C:\test\testfile.xml" --ftp-create-dirs
Download using curl on SFTP
curl -k "sftp://83.46.38.23:22/CurlPutTest/testfile.xml" --user "testuser:testpassword" -o "C:\test\testfile.xml" --ftp-create-dirs
Rename using curl on SFTP
curl -k "sftp://83.46.38.23:22/CurlPutTest/" --user "testuser:testpassword" -Q "-RENAME
‘/CurlPutTest/testfile.xml’ ‘/CurlPutTest/testfile.xml.tmp’" --ftp-create-dirs
Delete using curl on SFTP
curl -k "sftp://83.46.38.23:22/CurlPutTest/ " --user "testuser:testpassword" -Q "–RM /CurlPutTest/testfile.xml" --ftp-create-dirs
Make directory using curl on SFTP
curl -k "sftp://83.46.38.23:22/CurlPutTest/test " --user "testuser:testpassword" -Q "–MKDIR /CurlPutTest/Test" --ftp-create-dirs
Remove directory using curl on SFTP
curl -k "sftp://83.46.38.23:22/CurlPutTest/test " --user "testuser:testpassword" -Q "–RMDIR /CurlPutTest/Test" --ftp-create-dirs
Execute Curl By C# Code
- public bool ExecuteCommand(string curlExePath, string commandLineArguments, bool isReturn = true)
- {
- bool result = true;
- try
- {
- commandProcess = new Process();
- commandProcess.StartInfo.UseShellExecute = false;
- commandProcess.StartInfo.FileName = curlExePath;
- commandProcess.StartInfo.Arguments = commandLineArguments;
- commandProcess.StartInfo.CreateNoWindow = true;
- commandProcess.StartInfo.RedirectStandardInput = true;
- commandProcess.StartInfo.RedirectStandardOutput = true;
- commandProcess.StartInfo.RedirectStandardError = true;
- commandProcess.Start();
- var reader = new ProcessOutputReader(commandProcess);
- reader.ReadProcessOutput();
- commandProcess.WaitForExit();
- string output = reader.StandardOutput;
- lastStandardOutput = output;
- string error = reader.StandardError;
- commandProcess.Close();
- if (isCheck == true)
- {
- if (ComponentType.Equals("1"))
- {
- if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTP"))
- {
- if (lastStandardOutput.ToLower().Contains("content-length") && lastStandardOutput.ToLower().Contains("last-modified"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("SFTP"))
- {
- if (error.ToLower().Contains("could not open directory for reading: no such file or directory") || error.ToLower().Contains(" could not open remote file for reading: no such file or directory"))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTPS"))
- {
- if (lastStandardOutput.ToLower().Contains("content-length") && lastStandardOutput.ToLower().Contains("last-modified"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- else if (ComponentType.Equals("2"))
- {
- if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTP"))
- {
- if (error.ToLower().Contains("server denied you to change to the given directory"))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("SFTP"))
- {
- if (error.ToLower().Contains("could not open directory for reading: no such file or directory"))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTPS"))
- {
- if (error.ToLower().Contains("server denied you to change to the given directory"))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
- }
- else
- {
- if (error.ToLower().Contains("curl"))
- {
- LogError = error.Substring(error.LastIndexOf("curl"), 10).ToString();
- }
- }
- if (isReturn) result = LoggedError(error);
- return result;
- }
- catch (Exception ex)
- {
- _errorMessage = ex.ToString();
- Program.writeLogFile("|LauncherService|ERROR |" + _errorMessage, false);
- result = false;
- }
- return result;
- }
Thanks for reading this article, hope you enjoyed it.

Hemant MahajanPosted Feb 3, 2016, 5:33 AM
CURL is indeed a very useful tool. I use it for read WebAPI responses..
Santhakumar MunuswamyPosted Dec 1, 2015, 3:28 AM
Good One
Yaduveer SainiPosted Nov 30, 2015, 4:16 AM
Very nice article mukesh
Humayun Kabir MamunPosted Nov 25, 2015, 12:58 AM
Nice...
Sanjay SabariyaPosted Nov 24, 2015, 11:36 PM
great ...
Mukesh KumarPosted Nov 24, 2015, 9:08 AM
Thanks to every one
Raja TPosted Nov 24, 2015, 8:15 AM
Nice, Thanks for sharing
Shridhar SharmaPosted Nov 24, 2015, 6:53 AM
nice share
Suman VermaPosted Nov 24, 2015, 5:11 AM
nice article. thanks for sharing
Mukesh KumarPosted Nov 24, 2015, 5:04 AM
Thanks to all
Sridhar SharmaPosted Nov 24, 2015, 5:01 AM
Thanks for sharing
Upendra Pratap ShahiPosted Nov 24, 2015, 4:27 AM
nice info..
Banketeshvar NarayanPosted Nov 24, 2015, 4:13 AM
Good One
Harshad PansuriyaPosted Nov 24, 2015, 3:56 AM
Nice one