T
TK
I have a trouble to get web resopnse from an aspx page which is secured by
Forms Authentication with custom user account database.
My client application is a console application but not a browser. I want to
download a file from my webapplication.
I've learned that the NetworkCredential class gives a way to go but no luck.
My code is as following...just dump out the web response for debugging.
// C#
public void Download(string username, string password, string filename)
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("http://myserver/myapp/download.aspx?file=
" + filename);
req.Method = "GET";
req.Credentials = new NetworkCredential(username, password);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
char[] buf = new char[256];
int count;
while((count = sr.Read(buf, 0, 256)) > 0)
{
string s = new string(buf, 0, count);
Console.Write(s);
}
res.Close();
}
It seems the NetworkCredential doesn't work because above code dumps the
html document of the login page.
How can I accomplish this?
Please help!
best regards,
TK
Forms Authentication with custom user account database.
My client application is a console application but not a browser. I want to
download a file from my webapplication.
I've learned that the NetworkCredential class gives a way to go but no luck.
My code is as following...just dump out the web response for debugging.
// C#
public void Download(string username, string password, string filename)
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("http://myserver/myapp/download.aspx?file=
" + filename);
req.Method = "GET";
req.Credentials = new NetworkCredential(username, password);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
char[] buf = new char[256];
int count;
while((count = sr.Read(buf, 0, 256)) > 0)
{
string s = new string(buf, 0, count);
Console.Write(s);
}
res.Close();
}
It seems the NetworkCredential doesn't work because above code dumps the
html document of the login page.
How can I accomplish this?
Please help!
best regards,
TK