Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to download a file from a server using C# console application. Below is the code that I have written to achieve the same.
C#
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileLocation);
                request.Credentials = new NetworkCredential(userName, password, domainName);
                request.AllowAutoRedirect = true;
                WebResponse response = (HttpWebResponse)request.GetResponse();
                using (FileStream stream = new FileStream(Path.Combine(currentDirectory, fileName), FileMode.Create, FileAccess.Write))
                {
                    response.GetResponseStream().CopyTo(stream);
                }
                response.Close();

But the server where the file resides is a having "Site Minder" agent running and hence the HttpWebRequest has to pass through the site minder's authentication. Presently the request is being redirected to the web single sign on page(login page of the site minder agent) and is not able to pass through it.

I am having access to the server/file and I can download the file through the browser. But I want to have this done through the code.

Could someone please help me with the piece code to achieve the same ??.. Thanks in advance.
Posted
Updated 5-Jan-12 21:17pm
v2

1 solution

Take a look at this article on CP - How to Connect to a SiteMinder Protected Resource Using an HTTP Request[^].

Hope this helps
 
Share this answer
 
Comments
Kalmesh.Sam 6-Jan-12 4:44am    
Thanks for the reply. But I have already tried this code(pasted below) and no luck. Please review the code below and lemme know if any changes have to be made. Thanks.

HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(fileLocation);
httpReq.AllowAutoRedirect = false;

HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();

string siteURL = httpRes.Headers["Location"];//returns the URL of Login page with Target field in it referring to exact file locations

HttpWebRequest httpReq2 = (HttpWebRequest)HttpWebRequest.Create(siteURL);
httpReq2.AllowAutoRedirect = false;
httpReq.Method = "POST";
string postData = "";
postData += "USER=" + HttpUtility.UrlEncode(userName) + "&";
postData += "PASSWORD=" + HttpUtility.UrlEncode(password);
byte[] data = System.Text.Encoding.ASCII.GetBytes(postData);
CookieContainer cc = new CookieContainer();
httpReq2.CookieContainer = cc;
httpReq2.ContentType = "application/x-www-form-urlencoded";
httpReq2.ContentLength = data.Length;
Stream sw = httpReq2.GetRequestStream();
sw.Write(data, 0, data.Length);
sw.Close();
WebResponse response = (HttpWebResponse)httpReq2.GetResponse();
using (FileStream stream = new FileStream(Path.Combine(currentDirectory, fileName), FileMode.Create, FileAccess.Write))
{
response.GetResponseStream().CopyTo(stream);
}
response.Close();

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