Hej igen jeg har lavet et delvist eksempel til dig:
webservice:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Net;
[WebService(Namespace = "
http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public byte[] GetFile(string username, string password, string ftpsite, string fileName, string port)
{
Uri u = new Uri("
ftp://" + username + ":" + password + "@" + ftpsite + ":" + port + "/" + fileName);
if (u.Scheme != Uri.UriSchemeFtp)
{
return null;
}
WebClient request = new WebClient();
try
{
byte[] newFileData = request.DownloadData(u.ToString());
System.Diagnostics.Debug.WriteLine(System.Text.Encoding.UTF8.GetString(newFileData));
return newFileData;
}
catch (WebException e)
{
return null;
}
}
[WebMethod]
public void UploadFile(string username, string password, string ftpsite, string fileName, string port, byte[] file)
{
FtpWebRequest ftr;
ftr = (FtpWebRequest)FtpWebRequest.Create("
ftp://" + username + ":" + password + "@" + ftpsite + ":" + port + "/" + fileName);
ftr.Timeout = 10000;
ftr.ReadWriteTimeout = 10000;
ftr.KeepAlive = false;
ftr.UseBinary = true;
ftr.Method = WebRequestMethods.Ftp.UploadFile;
ftr.ContentLength = file.Length;
Stream requestStream = ftr.GetRequestStream();
requestStream.Write(file, 0, file.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)ftr.GetResponse();
System.Diagnostics.Debug.WriteLine("Append status: {0}", response.StatusDescription);
response.Close();
}
[WebMethod]
public string ListFolders(string username, string password, string ftpsite, string Folder, string port)
{
FtpWebRequest ftr;
ftr = (FtpWebRequest)FtpWebRequest.Create("
ftp://" + username + ":" + password + "@" + ftpsite + ":" + port + "/" + Folder);
ftr.Timeout = 10000;
ftr.ReadWriteTimeout = 10000;
ftr.KeepAlive = false;
ftr.UseBinary = true;
ftr.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftr.GetResponse();
Stream s = response.GetResponseStream();
StreamReader reader = new StreamReader(s);
string responseFromServer = reader.ReadToEnd();
response.Close();
return responseFromServer;
}
}