Mai Hu Na

Mai Hu Na

  • NA
  • 86
  • 133.2k

Download file using Webclient shows Wrong Data

Jul 15 2013 3:58 AM
hi Guyes,
              i used following code to download file from secured site but file created on folder @"C:\Downloads\Filename" show HTML tags like This

<HTML>
<HEAD><LINK rel="stylesheet" type="text/css" name="defaultstyle" href="/cehttp/html/style.css">
<TITLE>Servlet Response Message</TITLE></HEAD>
<BODY>
<H2 align=center><H2>
<HR>
You are not logged on. Please logon first.
<HR>
</BODY></HTML>

then Actual File content.   
           
     try
            {
                WebClient client = new WebClient();
                client.Credentials = new System.Net.NetworkCredential("username", "password");
                byte[] pageData = client.DownloadData("url");
                string pageHtml = Encoding.ASCII.GetString(pageData);
                Console.WriteLine(pageHtml);

                client.DownloadFile("url", @"C:\Downloads\071213.ACCT");
            }
            catch (WebException webEx)
            {
                Console.WriteLine(webEx.ToString());
                if (webEx.Status == WebExceptionStatus.ConnectFailure)
                {
                    Console.WriteLine("Are you behind a firewall?  If so, go through the proxy server.");
                }
            }

Answers (1)