I have a request in my project where I need to login to an external website and needs to download a file from the website. I don't have any access to the website except username/password to login. Is there any way to achieve this functionality using C#. Thanks!
-
2Yes there are many ways. But what do you expect from us? Write it for you? – L.B Dec 15 '12 at 19:05
-
Hi,I don't want you to write the code,just need suggestion on how to achieve this.in this requirement I need to open a bank web site and log in to the site using uname and pwd then down load the file by clicking a button on the site.till now this is been written with scripting language based on co ordinated of text box and button.But that is not so efficient as we need to change The script whenever lay out changes. – Ravi B Dec 16 '12 at 18:20
3 Answers
It depends on kind of website and authentication:
1) Using WebClient class
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(username, password);
wc.DownloadFile(url, "c:\\");
2) If the first didn't work, you can achieve it using webbrowser control
HtmlElement ele = webBrowser1.Document.GetElementById("email");
if (ele != null)
ele.InnerText = "username";
ele = webBrowser1.Document.GetElementById("pass");
if (ele != null)
ele.InnerText = "pass";
ele = webBrowser1.Document.GetElementById("Login");
if (ele != null)
ele.InvokeMember("click");

- 12,769
- 10
- 63
- 83
-
You can post values with the WebClient via [UploadValues](http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx) Also see [how to post data to specific URL using WebClient in C#](http://stackoverflow.com/q/5401501/119477) – Conrad Frix Dec 15 '12 at 23:19
You can use the WebBrowser control together with WATIN library to navigate through the site and click the download button.
WebBrowser control has a "Download file" event you can handle

- 8,951
- 10
- 69
- 111
If you can do it manually, you can do it programatically with C# or any other language that supports HTTP requests. All you need is :
Figure out how to authenticate correctly to the server. Once this done you will get an authentication token and an associated session with this token. You will then attach the token to all the subsequent requests to the server.
Figure out which request to send to get that particular resource. I would assume it is GET request for a specific URL. Use Fiddler to check which requests are sent if you do it manually.

- 35,458
- 16
- 93
- 163