I have searched various forums and links but havn’t got anything helpful yet.
Actually i working towards developing a web application in ASP.net using C# that enables use to login on a remote server and then download files from that server.
Curently i m able to login to that server and see the content of the protected page.
But when it comes to download the file, i get an Unauthorize access error.
I m able to download images from the server but those are not protected.
the code i have developed so far is
protected void Unnamed1_Click(object sender, EventArgs e)
{
try
{
string LOGIN_URL = "https://some.server/";
// first, request the login form to get the value
HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.KeepAlive = true;
webRequest.UserAgent = userAgent;
String received = Encoding.ASCII.GetString(Request.BinaryRead(Request.ContentLength));
webRequest.ContentLength = received.Length;
webRequest.Proxy.Credentials = new NetworkCredential("usrname", "password", "domain");
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream(), Encoding.ASCII);
string responseData = responseReader.ReadToEnd();
responseReader.Close();
string postData = "user=usr&password=pass&switch=Login";
Response.Write(webRequest.Address);
// have a cookie container ready to receive the forms auth cookie
CookieContainer cookies = new CookieContainer();
// now post to the login form
webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();
// Download files
WebProxy myProxy = new WebProxy("server.https.com", 443);
//WebRequest request = WebRequest.Create("https://some.server/file.zip");
WebRequest request = WebRequest.Create("https://some.server/file.zip");
request.Proxy.Credentials = new NetworkCredential("usrname", "password", "domain");
string filename = "file.zip";
string filepath = "C:\\Documents and Settings\\user\\Desktop\\" + filename.ToString();
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("usrname", "password", "domain");
client.DownloadFile("https://some.server/file.zip", filepath);
}
catch (Exception exp)
{
Response.Write(exp.ToString());
}
}
Server Error in ‘/WebSite1’ Application.
The remote server returned an error: (401) Unauthorized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
Source Error:
Line 80: WebClient client = new WebClient();
Line 81: client.Credentials = new NetworkCredential(“username”, “password”, “domain”);
Line 82: client.DownloadFile(“https://some.server/file.zip”, filepath);
Use this class derived from WebClient. It will always pass the cookies with every request.