lolliloop
lolliloop

Reputation: 399

Accessing FTP folder in c#

I want to access/ read text file from a ftp site but I encountered an error say's:

Unable to cast object of type 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest'.

Here's my code in C#:

string username = "username";
string password = "password";

FtpWebRequest tmpReq = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri("http://crmweb.com.ph/ftp/ftpuser1/User/tbl_users.csv"));
tmpReq.Credentials = new System.Net.NetworkCredential(username, password);

Upvotes: 1

Views: 2056

Answers (1)

Marco
Marco

Reputation: 23935

There are 2 possible errors in your code:

  1. You are using the wrong type, but the right URI
  2. You are using the right type, but the wrong URI

1.: If the address and protocol (http) are correct you have to use HttpWebRequest:

var tmpReq = HttpWebRequest.Create(new Uri("http://crmweb.com.ph/ftp/ftpuser1/User/tbl_users.csv"));

2.: If you want to open an FTP connection, you have to specify a valid ftp-address (use ftp://)

var ftpReq =  FtpWebRequest.Create(new Uri("ftp://bulk.resource.org"))

Upvotes: 2

Related Questions