Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public class CookiesAwareWebClient : WebClient
    {
        public CookieContainer CookieContainer;
        public CookieContainer MyProperty
	    {
		    get { return CookieContainer;}
		    set { CookieContainer = value;}
	    }
        public CookiesAwareWebClient()
        {
            CookieContainer = new CookieContainer();
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            ((HttpWebRequest)request).CookieContainer = CookieContainer;
            return request;
        }
    } 

 private void button1_Click(object sender, EventArgs e)
        {
textBox1.Text = @"http://www.codeproject.com/KB/IP/SimpleFTPDemo/SimpleFTP20_src.zip";
                        using (CookiesAwareWebClient client = new CookiesAwareWebClient()) 
            { 
                NameValueCollection values = new NameValueCollection();
                values.Add("Email", "abc@gmail.com");
                values.Add("password", "abc123" );
                values.Add("x", "10" ); // <- I doubt the server cares about the x position of where the user clicked on the image submit button :-)
                values.Add("y", "10"); // <- I doubt the server cares about the y position of where the user clicked on the image submit button :-) 
                values.Add("login", "login");
     
                // We authenticate first 
                client.UploadValues("https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f", values);

                // Now we can download 
                client.DownloadFile(textBox1.Text,"D:\1\23.zip")
        }
Posted
Updated 14-Aug-12 5:37am
v2
Comments
ravikaliappan 9-Aug-12 1:58am    
pls any body give solution for this

1 solution

The problem is the url for download is not appropriate when you use it on non-browser..

Also spelling mistake on html input "Password"


Let me post a fully flexible code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace CodeProjectDownload
{
    class Program
    {
        static void Main(string[] args)
        {
            Download("xyx@domain.com", "mypassword", @"C:\Test", @"http://www.codeproject.com/KB/IP/SimpleFTPDemo/SimpleFTP20_src.zip");
        }

        private static void Download(string email, string password, string destination, string downloadUri)
        {

            using (CookiesAwareWebClient client = new CookiesAwareWebClient())
            {
                NameValueCollection values = new NameValueCollection();
                values.Add("Email",email );
                values.Add("Password", password);
                values.Add("x", "10"); // <- I doubt the server cares about the x position of where the user clicked on the image submit button
                values.Add("y", "10"); // <- I doubt the server cares about the y position of where the user clicked on the image submit button
                values.Add("login", "login");

                // We authenticate first
                client.UploadValues("https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f", values);

                // Now we can download

                string modifieduri = "http://www.codeproject.com/script/articles/download.aspx?file=" + downloadUri.Replace("http://www.codeproject.com","") + "&rp=";


                string filename = System.IO.Path.GetFileName(downloadUri);

                string filepathname = System.IO.Path.Combine(destination, filename);

                client.DownloadFile(modifieduri, filepathname);
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900