mercredi 17 juillet 2019

Download text file from website using WebCLient

We recently switched our ftp site at work from ftp://address to https://ftp.address and now my application that retrieves files is unable to download files with expected content.

I previously used FtpWebRequest and it has been working for many years. I am now trying to use WebClient and it downloads a file but it is not the text file I need. Instead, the contents of the file turns out to be some HTML.

Previous that worked:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(getAppSetting("URI") + getAppSetting("FilePath") + args[0].ToString());

request.Method = WebRequestMethods.Ftp.DownloadFile;

setCredentials(request);

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

Changed code to handle https:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

WebClient request = new WebClient();

setCredentials(request);

string file = request.DownloadString(getAppSetting("URI") + getAppSetting("FilePath") + args[0].ToString());

The result is a file that is downloaded but it contains HTML. I expect to have the contents of the file on the website I am pulling from.

Aucun commentaire:

Enregistrer un commentaire