Upload using ftp

mj1223

New member
Joined
Mar 11, 2021
Messages
2
Programming Experience
10+
I have created a console application to send an upload of job postings to monster.com the requirements of the appliction are to send a JSon feed of each open position via ftp to them. To handle this i created 3 classes. the first class is the JobPostings which has 8 properties and 1 method (GetPostings), then there is the Feed class which has 5 properties and no methods and lastly the JobFeed class which has 1 property. Once it creates the json feed it is saved to a directory on the computer and then I create an instance of the WebClient class to upload to Monster. Right now I am telling it to write to the console after it completes making the file and then after uploading. Which it is doing , but if I use another program to look in the ftp directory it is empty. By looking at my code can you tell me what I have done wrong

Here is my code
default program:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO;
using System.Net;
using System.Net.Mail;


namespace MonsterOrganicJobs
{
    class Program
    {
        //static void Main(string[] args)
        static void Main()
        {
            string today = DateTime.Today.ToShortDateString();
            string path = @"X:\MonsterFeed\";             //@"E:\WebPages\MonsterFeed\";  <-- once working properly we will use this directory
            string fileName = today.Replace("/", "-") + ".json";

            try
            {
                //Get the postings and serialize as json
                JobPosting jobPosting = new JobPosting();
                List<JobPosting> myList = jobPosting.GetPostings();
                string jsonString = JsonSerializer.Serialize(myList);

                //create the json feed
                Feed feed = new Feed
                {
                    isfullfeed = true,
                    islast = true,
                    part = 1,
                    provider = "Company name here",
                    jobs = jsonString
                };
                string myfeed = JsonSerializer.Serialize(feed);

                //create the json jobfeed
                JobFeed jobFeed = new JobFeed
                {
                    jobfeed = myfeed
                };
                byte[] jobfeed = JsonSerializer.SerializeToUtf8Bytes(jobFeed);
                string thefeed = Encoding.UTF8.GetString(jobfeed);

                string FileLocation = path + fileName;

                //Create the file
                File.WriteAllText(FileLocation, thefeed);
                Console.WriteLine("file saved to " + FileLocation);
                //send to monster using ftp
                using (var client = new WebClient())
                {
                    client.Credentials = new NetworkCredential("myID", "MyPassword");
                    client.UploadFile("ftp.monster.com", WebRequestMethods.Ftp.UploadFile, FileLocation);
                    Console.WriteLine("File uploaded.");
                    Console.ReadKey();
                }
            }
            catch (Exception e)
            {
                SendAnEmail(e.ToString(), "Error Uploading file to Monster");
                Console.WriteLine(e);
                Console.ReadKey();
            }
        }

        public static void SendAnEmail(string message, string subject)
        {
            using (MailMessage msg = new MailMessage("Sending_Email_Address", "mj_Email_Address"))
            {
                msg.Subject = subject;
                msg.Body = message;
                msg.BodyEncoding = System.Text.UTF8Encoding.UTF8;
                msg.SubjectEncoding = System.Text.Encoding.Default;
                msg.IsBodyHtml = true;
                using (var smtp = new SmtpClient("mail.CompanyName.net"))
                {
                    NetworkCredential networkCredential = new NetworkCredential("Sending_Email_Address", "Email_password");
                    smtp.EnableSsl = false;
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = networkCredential;
                    smtp.Send(msg);
                }
            }
         
        }
    }
}

Here is a link to the monster specs https://partner.monster.com/job-ads-non-duration
 
Solution
Address must include protocol and filename to be uploaded. You could also use client.BaseAddress for the directory and just set filename in address.
WebClient.UploadFile() returns a response. What is in the response that you get back? Does it indicate success?

Also, I thought that FTP URLs started with "ftp://". I maybe mistaken, though.

Also, my reading of the document you linked to says that you need to upload to a specific location. I'm not seeing how you are doing that. It looks like you are just uploading to the root of the FTP location. I maybe mistaken, though since I am trying to read code on a small phone.
 
Address must include protocol and filename to be uploaded. You could also use client.BaseAddress for the directory and just set filename in address.
 
Solution
when I added the protocol and the file name ftp://ftp.monster.com/3-15-2021.json it refused to connect

so I switched the code to be this and now it is working properly

mycode:
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.monster.com/test/inbound/"+fileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("My_ID", "My_Password");

                // Copy the contents of the file to the request stream.
                byte[] fileContents;
                using (StreamReader sourceStream = new StreamReader(FileLocation))
                {
                    fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                }

                request.ContentLength = fileContents.Length;

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(fileContents, 0, fileContents.Length);
                }

                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    SendAnEmail(response.StatusDescription, "Uploaded file to Monster is completed");
                    //Console.WriteLine($"Upload File Complete, status {response.StatusDescription}");
                    //Console.ReadKey();
                }
 
Notice that the URL you used for that working FTP web request is different from the "ftp://ftp.monster.com/3-15-2021.json" that you tested with using the web client. Under the covers, web client just calls FTP web request when appropriate.
 

Latest posts

Back
Top Bottom