本文整理汇总了C#中System.Net.FtpWebRequest类的典型用法代码示例。如果您正苦于以下问题:C# FtpWebRequest类的具体用法?C# FtpWebRequest怎么用?C# FtpWebRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FtpWebRequest类属于System.Net命名空间,在下文中一共展示了FtpWebRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DownloadFtp
/// <summary>
/// 连接FTP的方法
/// </summary>
/// <param name="ftpuri">ftp服务器地址,端口</param>
/// <param name="ftpUserID">用户名</param>
/// <param name="ftpPassword">密码</param>
public DownloadFtp(string ftpuri, string ftpUserID, string ftpPassword)
{
// 根据uri创建FtpWebRequest对象
ft = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpuri));
// ftp用户名和密码
ft.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ft.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
fr = (FtpWebResponse)ft.GetResponse();
stream = fr.GetResponseStream();
////二进制文件读入
//if (!fr.ContentType.ToLower().StartsWith("text/"))
//{
// SaveBinaryFile(fr);
//}
////文本文件
//else
//{
string buffer = "", line;
StreamReader reader = new StreamReader(stream);
while ((line = reader.ReadLine()) != null)
{
buffer += line + "\r\n";
}
//装入整个文件之后,接着就要把它保存为文本文件。
SaveTextFile(buffer);
//}
}
示例2: setProxyInfo
private static void setProxyInfo(SetupConf setVariables, FtpWebRequest request)
{
try
{
string strProxyServer = setVariables.objProxyVariables.proxyServer.ToString();
string strProxyPort = setVariables.objProxyVariables.proxyPort.ToString();
string strProxyUsername = setVariables.objProxyVariables.proxyUsername.ToString();
string strProxyPassword = setVariables.objProxyVariables.proxyPassword.ToString();
IWebProxy proxy = request.Proxy;
if (proxy != null)
{
Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
}
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri(Uri.UriSchemeHttp + "://" + strProxyServer + ":" + strProxyPort);
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential(strProxyUsername, strProxyPassword);
request.Proxy = myProxy;
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
Console.ReadLine();
System.Environment.Exit(-1);
}
}
示例3: Upload
/// <summary>
/// 上傳檔案至FTP
/// </summary>
/// <param name="FilePath">要上傳的檔案來源路徑</param>
/// <param name="FileName">要上傳的檔案名稱</param>
/// <returns></returns>
public static bool Upload(string FilePath,string FileName)
{
Uri FtpPath = new Uri(uriServer+FileName);
bool result = false;
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(FtpPath);
ftpRequest.Credentials = new NetworkCredential(UserName, Password);
ftpRequest.UsePassive = false;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpFileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
byte[] uploadBytes = new byte[ftpFileStream.Length];
ftpFileStream.Read(uploadBytes, 0, uploadBytes.Length);
ftpStream = ftpRequest.GetRequestStream();
ftpStream.Write(uploadBytes, 0, uploadBytes.Length);
ftpFileStream.Close();
ftpStream.Close();
ftpRequest = null;
result = true;
}
catch (WebException ex)
{
sysMessage.SystemEx(ex.Message);
}
return result;
}
示例4: uploadData
/// <summary>
/// Method to upload data to an ftp server
/// </summary>
public void uploadData()
{
Console.WriteLine("*** Beginning File Upload(s) ***");
foreach (string afile in files)
{
Console.WriteLine("\t...writing out " + afile);
//create a connection object
ftpServer = connectionInfo.Split('|')[0];
request = (FtpWebRequest)WebRequest.Create(new Uri(ftpServer + "/" + afile));
request.Method = WebRequestMethods.Ftp.UploadFile;
//set username and password
userName = connectionInfo.Split('|')[1];
userPassword = connectionInfo.Split('|')[2];
request.Credentials = new NetworkCredential(userName, userPassword);
//copy contents to server
sourceStream = new StreamReader(programFilesAt + afile);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
response = (FtpWebResponse)request.GetResponse();
response.Close();
}
Console.WriteLine("\t File Uploads Complete");
}
示例5: init
private static void init(string method)
{
request = (FtpWebRequest) WebRequest.Create("ftp://" + server + path + file_name);
request.Method = method;
request.Credentials = new NetworkCredential("anonymous", "password");
}
示例6: GetVersion
private static Boolean GetVersion(FtpWebRequest request)
{
Console.WriteLine("Checking Local Version...");
var f = new StreamReader(@".\Connections\version.json");
string data = f.ReadToEnd();
versionsInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<VersionInfo>(data);
credentials = new NetworkCredential(versionsInfo.ServerUserName,versionsInfo.ServerPassword);
Console.WriteLine("Connecting to server...");
request = (FtpWebRequest)WebRequest.Create(versionsInfo.ServerUrl + "Version.json");
request.Credentials = credentials;
request.Method = WebRequestMethods.Ftp.DownloadFile;
Console.WriteLine("Downloading...");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
serverVersion = Newtonsoft.Json.JsonConvert.DeserializeObject<VersionInfo>(reader.ReadToEnd());
Console.WriteLine("Close Server Connections...");
reader.Close();
response.Close();
Console.WriteLine("Server Version : " + serverVersion.Version);
Console.WriteLine("Local Version : " + versionsInfo.Version);
f.Close();
return versionsInfo.Version < serverVersion.Version;
//Console.WriteLine(versionsInfo.Version);
//f.ToString();
}
示例7: DownloadFile
public void DownloadFile(string remoteFile, string localFile)
{
// Создаем FTP Request
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(String.Format("{0}/{1}", host, remoteFile));
// Инициализируем сетевые учетные данные
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
// Задаем команду, которая будет отправлена на FTP-сервер
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.Timeout = TIMEOUT;
// Ответ FTP-сервера
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
// Возвращаем поток данных
ftpStream = ftpResponse.GetResponseStream();
// Создаем локальный файл
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
// Пишем в файл
ftpStream.CopyTo(localFileStream);
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
示例8: createDirectory
/* Create a New Directory on the FTP Server */
public bool createDirectory(string newDirectory)
{
bool result = true;
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Resource Cleanup */
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex)
{
StockLog.Write(ex.ToString());
result = false;
}
return result;
}
示例9: copyToServer
public bool copyToServer(string remote, string local, string logPath)
{
bool success = false;
// try
// {
ftpRequest = (FtpWebRequest)WebRequest.Create(host + remote);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Credentials = netCreds;
byte[] b = File.ReadAllBytes(local);
ftpRequest.ContentLength = b.Length;
using (Stream s = ftpRequest.GetRequestStream())
{
s.Write(b, 0, b.Length);
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
// check was successful
if (ftpResponse.StatusCode == FtpStatusCode.ClosingData)
{
success = true;
logResult(remote, host, user, logPath);
}
ftpResponse.Close();
/* }
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}*/
return success;
}
示例10: FtpWebResponse
internal FtpWebResponse (FtpWebRequest request, Uri uri, string method, bool keepAlive)
{
this.request = request;
this.uri = uri;
this.method = method;
//this.keepAlive = keepAlive;
}
示例11: VerificaSeExiste
public bool VerificaSeExiste(string destinfilepath, string ftphost, string ftpfilepath, string user, string pass)
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://" + ftphost + "//" + ftpfilepath);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
try {
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
return true;
}
catch (Exception exc)
{
this.sErro = exc.Message;
return false;
}
}
示例12: Connect
private void Connect(string Path)
{
this.ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(Path));
this.ftpRequest.UseBinary = true;
this.ftpRequest.UsePassive = true;
this.ftpRequest.KeepAlive = this._isKeepAlive;
this.ftpRequest.Credentials = new NetworkCredential(this._userID, this._passWord);
}
示例13: setProxyAuthorization
private static void setProxyAuthorization(SetupConf setVariables, FtpWebRequest request)
{
//Proxy configurations
if (setVariables.objProxyVariables.proxyServer.ToString() != "PlaceHolder" && setVariables.objProxyVariables.proxyPort.ToString() != "PlaceHolder")
{
setProxyInfo(setVariables, request);
}
}
示例14: FTP
public FTP(string requestUriString, string ftpUserID, string ftpPassword)
{
ftpRequest = FtpWebRequest.Create(requestUriString) as FtpWebRequest;
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftpRequest.KeepAlive = false;
ftpRequest.UsePassive = false;
ftpRequest.UseBinary = true;
}
示例15: RemoveDirectory
public void RemoveDirectory(string ftpAddressPath, string directoryName)
{
this.FtpWebRequest = (FtpWebRequest)FtpWebRequest.Create(ftpAddressPath + directoryName);
this.FtpWebRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
this.FtpWebRequest.Credentials = new NetworkCredential(this.Username, this.Password);
this.FtpWebRequest.GetResponse();
}