From: Parrot on
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();

From: Harlan Messinger on
Parrot wrote:
> 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]
From: Parrot on
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" wrote:

> Parrot wrote:
> > 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]
> .
>
From: Parrot on
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" wrote:

> Parrot wrote:
> > 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]
> .
>
From: Harlan Messinger on
Parrot wrote:
> 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.