Uploading file to UNIX host

P

Parrot

Does anyone have code to upload a file from a Windows PC to a Unix Host? I
tried using the following code and get an error saying 'Unable to cast object
type 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest'. This
code works when uploading to the internet but not to a Unix host computer.
The ftpside variable is a numeric url address such as 10.20.30.115. Are
there different parameters to use?

string fileName = Directory.GetCurrentDirectory() + "/gctrans.dat";

string ftpsite = ftpserver + "/gctrans.dat";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpsite);
request.Method = WebRequestMethods.Ftp.UploadFile;

StreamReader sourceStream = new StreamReader(fileName);
byte[] fileContents =
Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

request.AuthenticationLevel =
System.Net.Security.AuthenticationLevel.None;

request.Credentials = new NetworkCredential(UserID, UserPassword);
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 
H

Harlan Messinger

Parrot said:
Does anyone have code to upload a file from a Windows PC to a Unix Host? I
tried using the following code and get an error saying 'Unable to cast object
type 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest'. This
code works when uploading to the internet but not to a Unix host computer.
The ftpside variable is a numeric url address such as 10.20.30.115. Are
there different parameters to use?

You don't have an ftpside variable.
string fileName = Directory.GetCurrentDirectory() + "/gctrans.dat";

string ftpsite = ftpserver + "/gctrans.dat";

If by "ftpside", you meant "ftpsite", then clearly your ftpsite variable
is NOT a numeric address such as 10.20.30.115. If by "ftpside", you
meant "ftpserver", so that you have ftpserver = "10.20.30.115" and
ftpsite = "10.20.30.115/gctrans.dat", then you haven't created a full
URL and I would expect either that WebRequest.Create(ftpsite) wouldn't
work, or else that it would assume by default that you meant the
protocol to be http, in which case it would generate an HttpWebRequest,
which isn't an FtpWebRequest and which would produce an error if you
tried to cast it into one.

If, instead, ftpserver = "ftp://10.20.30.115", then this might work.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpsite);
request.Method = WebRequestMethods.Ftp.UploadFile;
[snip]
 
P

Parrot

Thanks for your reply. When I test the routine I use an ftpsite with a url
value of http://www.mysite.com and it works. When my client uses the code he
enters the 10.20.30.115 url address. The fact that it works when I upload to
my site implies that I do not use an ftp site but rather a server site.
Perhaps my client is using a numeric url that is an ftp server rather than an
http server. Maybe he should be using a url for an http server rather than
an ftp server. Does that make sense?

Harlan Messinger said:
Parrot said:
Does anyone have code to upload a file from a Windows PC to a Unix Host? I
tried using the following code and get an error saying 'Unable to cast object
type 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest'. This
code works when uploading to the internet but not to a Unix host computer.
The ftpside variable is a numeric url address such as 10.20.30.115. Are
there different parameters to use?

You don't have an ftpside variable.
string fileName = Directory.GetCurrentDirectory() + "/gctrans.dat";

string ftpsite = ftpserver + "/gctrans.dat";

If by "ftpside", you meant "ftpsite", then clearly your ftpsite variable
is NOT a numeric address such as 10.20.30.115. If by "ftpside", you
meant "ftpserver", so that you have ftpserver = "10.20.30.115" and
ftpsite = "10.20.30.115/gctrans.dat", then you haven't created a full
URL and I would expect either that WebRequest.Create(ftpsite) wouldn't
work, or else that it would assume by default that you meant the
protocol to be http, in which case it would generate an HttpWebRequest,
which isn't an FtpWebRequest and which would produce an error if you
tried to cast it into one.

If, instead, ftpserver = "ftp://10.20.30.115", then this might work.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpsite);
request.Method = WebRequestMethods.Ftp.UploadFile;
[snip]
.
 
P

Parrot

I just found out that the user entered an http address rather than an ftp
address which probably caused the error. I emailed him back to enter an ftp
address and then try it. Since the client is in England I won't know until
tomorrow morning if it works.


Harlan Messinger said:
Parrot said:
Does anyone have code to upload a file from a Windows PC to a Unix Host? I
tried using the following code and get an error saying 'Unable to cast object
type 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest'. This
code works when uploading to the internet but not to a Unix host computer.
The ftpside variable is a numeric url address such as 10.20.30.115. Are
there different parameters to use?

You don't have an ftpside variable.
string fileName = Directory.GetCurrentDirectory() + "/gctrans.dat";

string ftpsite = ftpserver + "/gctrans.dat";

If by "ftpside", you meant "ftpsite", then clearly your ftpsite variable
is NOT a numeric address such as 10.20.30.115. If by "ftpside", you
meant "ftpserver", so that you have ftpserver = "10.20.30.115" and
ftpsite = "10.20.30.115/gctrans.dat", then you haven't created a full
URL and I would expect either that WebRequest.Create(ftpsite) wouldn't
work, or else that it would assume by default that you meant the
protocol to be http, in which case it would generate an HttpWebRequest,
which isn't an FtpWebRequest and which would produce an error if you
tried to cast it into one.

If, instead, ftpserver = "ftp://10.20.30.115", then this might work.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpsite);
request.Method = WebRequestMethods.Ftp.UploadFile;
[snip]
.
 
H

Harlan Messinger

Parrot said:
Thanks for your reply. When I test the routine I use an ftpsite with a url
value of http://www.mysite.com and it works. When my client uses the code he
enters the 10.20.30.115 url address. The fact that it works when I upload to
my site implies that I do not use an ftp site but rather a server site.

It's hard to figure out what's really going on because you're using
terminology incorrectly. An FTP server is just as much a server as an
HTTP server is.
Perhaps my client is using a numeric url that is an ftp server rather than an
http server. Maybe he should be using a url for an http server rather than
an ftp server. Does that make sense?

If you are trying to convert the request to an FtpWebRequest, then there
had *better* be an FTP server at the other end to respond to it.
 
K

Konrad Neitzel

Hi!
I just found out that the user entered an http address rather than an ftp
address which probably caused the error. I emailed him back to enter an ftp
address and then try it. Since the client is in England I won't know until
tomorrow morning if it works.

In your code you should always make sure, that the user entered something
valid.

If you cannot do that fully, then you should keep care of the possible
errors. (In your example, it is the cast to the FTP Request.)

It is always a good idea to make the application as "fool proven" as
possible (e.g. giving an error message: "You did not enter a valid FTP
Url." instead of an application crash!)

With kind regards,

Konrad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top