downloading a file HttpWebRequest c# need help

W

williamxifaras

I am trying to download a file from a secure web-site. However what is
getting downloaded is just the page html and not the actual file which
happens to be an xml file. Here is my code. Any suggestions would be
most appreciated. I changed the url for security reasons.

HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("https://somesite.com/reports/test.xml");
request.Credentials = new NetworkCredential("xxxx",
"xxxx");
request.UserAgent = "Mozilla/4.0+";
request.Accept = "*/*";
request.Headers.Add("Cache-Control", "no-cache");//
force no-caching
request.Method = "GET";

// Downloads the XML file from the specified server.
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();

stream = response.GetResponseStream();
byte[] inBuffer = ReadFully(stream, 32768);

string destFileName =
ConfigurationManager.AppSettings["Directory"].ToString() +
ConfigurationManager.AppSettings["FtpIn"].ToString() + nasdfile;
fstream = new FileStream(destFileName,
FileMode.OpenOrCreate, FileAccess.Write);
fstream.Write(inBuffer, 0, inBuffer.Length);
 
J

Jani Järvinen [MVP]

Hello,
I am trying to download a file from a secure web-site. However what is
getting downloaded is just the page html and not the actual file which
happens to be an xml file.

Does your site require authentication, i.e. you to type in your username and
password? If so, then this is the "HTML page" you will get when you try to
download your XML file.

If this is the case, you need to provide the credentials to access the site.
See for example this blog entry:

http://geekswithblogs.net/dtotzke/articles/24571.aspx

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top