Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 22nd, 2008, 06:27 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Issue in Upload/download FTP file via Proxy C#.NET

Subject: Issue in Upload/download FTP file via Proxy in ASP.NET 2.0 [C#]

Hello
How are you doing?

I am getting the following error when trying to FTP upload a file using FtpWebRequest via company proxy server.

"The requested FTP command is not supported when using HTTP proxy. "

Please suggest. as i am sure there should be a simple solution for this as it a very common proxy scenario

C# code is enclosed as below.

thanks

Naveen

using System.IO;
using System.Net;

namespace Bern.ISS.Web.Bloomberg
{
    public class requestproc
    {

        public requestproc()
        {
            this._ftpServer = ConfigurationManager.AppSettings["FTP.Server"];
            this._ftpPort = int.Parse(ConfigurationManager.AppSettings["FTP.Port"]);

            this._ftpUser = ConfigurationManager.AppSettings["FTP.User"];
            this._ftpPass = ConfigurationManager.AppSettings["FTP.Password"];
        }

        public static void SendRequest() //string filePath
        {
            try
            {
                string sFTPServer = "ftp://bfs.bloomberg.com/";
                string sRemoteFile = "eodpricing_I.req";
                string sURI = sFTPServer + sRemoteFile;
                string sReqFile = ConfigurationManager.AppSettings["Bloomberg.Draft.Path"] + "\\eodpricing_I.req";

                //Get the object used to communicate with the server
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sFTPServer);

                //Set the proxy server login information
                WebProxy wproxy = new WebProxy("ftppxy.xyzcompany.com");
                wproxy.Credentials = new NetworkCredential("pxyuserid", "pxypasswd");
                request.Proxy = wproxy;
                //request.Proxy = null;

                //Use logon information
                request.Credentials = new NetworkCredential("bloomberguserid", "bloombergpwd");
                request.KeepAlive = false;
                request.UseBinary = true;
                request.UsePassive = true;

                request.Method = WebRequestMethods.Ftp.UploadFile;
// request.Method = WebRequestMethods.Ftp.DownloadFile;
// request.Method = WebRequestMethods.Ftp.MakeDirectory;


                // Copy the contents of the file to the request stream.
// StreamReader sourceStream = new StreamReader(sReqFile);
// byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
// sourceStream.Close();
// request.ContentLength = fileContents.Length;

                FileStream streamObj = File.OpenRead(sReqFile);
                byte[] buffer = new byte[streamObj.Length];
                streamObj.Close();
                streamObj = null;

                //Upload File to ftp://localHost/ set its object to nothing
                request.GetRequestStream().Write(buffer, 0, buffer.Length);

// Stream requestStream = request.GetRequestStream();
// requestStream.Write(fileContents, 0, fileContents.Length);
// requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                response.Close();


            }
            catch (Exception exc)
            {
                throw exc;
                //throw new exception("Could not upload file to the FTP server.", exc);
            }
        }

    }
}


 
Old May 26th, 2008, 10:50 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello

Anybody has any idea using the .NET class library to call ftp upload/download functionality through a company proxy server.

Please let me know as I am looking for a solution.
If not a native .NET class library functionality, then any information on any 3rd party component control that can do the needful,. i.e. Call FTP upload/download via a proxy server in an ASP.NET C# page.

Thanks

 
Old May 27th, 2008, 02:54 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello

How are you doing?

I am looking for the following information to make a FTP call [via proxy] from a .NET program:

> The best practices followed for calling FTP through the corporate proxy server from a .NET C# Web page.
   Currently I tried using System.Net class library [.NET 2.0] to call ftp upload/download functionality through proxy, without success.

> Do we have some kind of Internal built-in native .NET class library functionality that address this issue?

> If not, then do we have any 3rd party component control that can call FTP upload/download via a proxy server in an ASP.NET C# page.

> Does FTP Voyager provide a class library to programmatically call FTP via proxy.

> Provide specific FTP Contact links on the web for further details.

Please let me know as I am looking for a solution to finalize our market data needs.

Thanks

 
Old June 20th, 2008, 08:51 AM
Authorized User
 
Join Date: Jan 2008
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Doom
Default

Found this example off the net. Its a windows form application not a console application. it will give you a rough idea of the syntax.

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Needed
using System.IO;
using System.Net;

namespace DownloadData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void lbVCKicks_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://vckicks.110mb.com");
        }

        private byte[] downloadedData;

        private void downloadData(string url)
        {
            WebClient downloadClient = new WebClient();

            downloadedData = new byte[0];
            try
            {
                //Optional
                this.Text = "Connecting...";
                Application.DoEvents();

                //Get a data stream from the url
                WebRequest req = WebRequest.Create(url);
                WebResponse response = req.GetResponse();
                Stream stream = response.GetResponseStream();

                //Download in chuncks
                byte[] buffer = new byte[1024];

                //Get Total Size
                string dataLengthString = response.Headers["Content-Length"];
                int dataLength = 0;
                if (dataLengthString != null)
                {
                    dataLength = Convert.ToInt32(dataLengthString);
                }

                //With the total data we can set up our progress indicators
                progressBar1.Maximum = dataLength;
                lbProgress.Text = "0/" + dataLength.ToString();

                this.Text = "Downloading...";
                Application.DoEvents();

                //Download
                MemoryStream memStream = new MemoryStream();
                while (true)
                {
                    //Try to read the data
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);

                    if (bytesRead == 0)
                    {
                        //Finished downloading
                        progressBar1.Value = progressBar1.Maximum;
                        lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString();

                        Application.DoEvents();
                        break;
                    }
                    else
                    {
                        //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);

                        //Update the progress bar
                        if (progressBar1.Value + bytesRead <= progressBar1.Maximum)
                        {
                            progressBar1.Value += bytesRead;
                            lbProgress.Text = progressBar1.Value.ToString() + "/" + dataLength.ToString();

                            progressBar1.Refresh();
                            Application.DoEvents();
                        }                        
                    }
                }

                //Convert the downloaded stream to a byte array
                downloadedData = memStream.ToArray();

                //Clean up
                stream.Close();
                memStream.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("There was an error accessing the url");
            }

            txtData.Text = downloadedData.Length.ToString();
            this.Text = "Download Data";
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            downloadData(txtUrl.Text);

            //We just want to extract the name for the save dialog
            if (downloadedData.Length != 0)
            {
                string urlName = txtUrl.Text;
                if (urlName.EndsWith("/"))
                    urlName = urlName.Substring(0, urlName.Length - 1); //Chop off the last '/'

                urlName = urlName.Substring(urlName.LastIndexOf('/') + 1);

                saveDiag1.FileName = urlName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (downloadedData.Length != 0)
            {
                if (saveDiag1.ShowDialog() == DialogResult.OK)
                {
                    this.Text = "Saving Data...";
                    Application.DoEvents();

                    //Write the bytes to a file
                    FileStream newFile = new FileStream(saveDiag1.FileName, FileMode.Create);
                    newFile.Write(downloadedData, 0, downloadedData.Length);
                    newFile.Close();

                    this.Text = "Download Data";
                    MessageBox.Show("Saved Successfully");
                }
            }
        }
    }
}
Here is the link for the snippet

http://vckicks.110mb.com/download_file_http.html




Similar Threads
Thread Thread Starter Forum Replies Last Post
ftp file upload yugi9966 ADO.NET 1 August 7th, 2008 09:09 AM
FTP connection to a Proxy ajaypsingh Pro Java 0 July 14th, 2006 06:36 AM
Upload Of Excel file to FTP Site shahbazr4 Java Databases 0 May 2nd, 2005 04:01 AM
Deleting file after ftp download otarboy Pro VB 6 0 September 27th, 2004 03:58 AM
Automate the ftp Upload of a Comma-Delineated File darinsee Access ASP 0 February 11th, 2004 10:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.