Click here to Skip to main content
15,905,971 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Help please! Pin
Christian Graus4-Oct-07 15:17
protectorChristian Graus4-Oct-07 15:17 
GeneralRe: C# Help please! Pin
Anthony Mushrow4-Oct-07 15:23
professionalAnthony Mushrow4-Oct-07 15:23 
AnswerRe: C# Help please! Pin
Guffa4-Oct-07 19:52
Guffa4-Oct-07 19:52 
QuestionComplex Regular Expression Pin
Skippums4-Oct-07 12:37
Skippums4-Oct-07 12:37 
AnswerRe: Complex Regular Expression Pin
MidwestLimey5-Oct-07 5:18
professionalMidwestLimey5-Oct-07 5:18 
GeneralRe: Complex Regular Expression Pin
Skippums5-Oct-07 5:36
Skippums5-Oct-07 5:36 
GeneralRe: Complex Regular Expression Pin
MidwestLimey5-Oct-07 6:08
professionalMidwestLimey5-Oct-07 6:08 
QuestionDownload File From Website With Login Pin
redfish344-Oct-07 11:39
redfish344-Oct-07 11:39 
I am attempting to automate the downloading of a file from a website that requires user login. At this point i want to get clarification of the steps i need to follow to accomplish this. Currently i am doing the following (in pseudo code) without success:

--Make POST HttpWebRequest/HttpWebResponse of "Login.php" that posts login username and password;
--Get response cookie collection and put into CookieContainer variable;
--Make HttpWebRequest/HttpWebResponse of "Content.php"; assign CookieContainer variable to request;
--Get GetResponseStream of response and put HTML content into String;
--Parse HTML content for download link;
--Download link to disk;
--Process downloaded file....

With this code, the HTML retrieved from Content.php is for a webpage shown to users that have not loged on and so the download link is not shown. Below is test code for the above.

// ##################################################################

public void TEST()
{
// login
// forms authenticate
string loginUri = AppGlobal.LoginUrl;
string requestString = "username=" + AppGlobal.UserName + "&password=" + AppGlobal.Password + "&autologin=checked";
byte[] requestData = Encoding.UTF8.GetBytes(requestString);

HttpWebRequest request = null;
Stream stream = null;
HttpWebResponse response = null;

// set up request
_cookies = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUri);
request.Proxy = null;
request.CookieContainer = _cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestData.Length;
// make form post
stream = request.GetRequestStream();
stream.Write(requestData, 0, requestData.Length);

// get response
response = (HttpWebResponse)request.GetResponse();

// update cookies
foreach (Cookie returnCookie in response.Cookies)
{
// debug help
Debug.WriteLine("COOKIE: " + returnCookie.Name + " = " + returnCookie.Value);

// compare new cookie with stored cookie
// update cookie container
bool cookieFound = false;
foreach (Cookie oldCookie in _cookies.GetCookies(new Uri(AppGlobal.CookieUrl)))
{
if (returnCookie.Name == oldCookie.Name)
{
oldCookie.Value = returnCookie.Value;
cookieFound = true;
}
}
if (cookieFound == false)
{
_cookies.Add(new Uri(AppGlobal.CookieUrl), returnCookie);
}
}

// get webpage
request = (HttpWebRequest)WebRequest.Create(new Uri(AppGlobal.StartUrl));
request.Proxy = null;
request.CookieContainer = _cookies;

// get response
// check link
response = (HttpWebResponse)request.GetResponse();
string statusCode = response.StatusCode.ToString();

if (statusCode == "OK")
{
// handle cookies
foreach(Cookie returnCookie in response.Cookies)
{
bool cookieFound = false;
foreach(Cookie oldCookie in _cookies.GetCookies(new Uri(AppGlobal.CookieUrl)))
{
if (returnCookie.Name == oldCookie.Name)
{
oldCookie.Value = returnCookie.Value;
cookieFound = true;
}
}
if (cookieFound == false)
{
_cookies.Add(returnCookie);
}
}

// handle redirection

// get link html
StreamReader reader = new StreamReader(response.GetResponseStream());
string content = reader.ReadToEnd();

// check for failed log in
int position = content.IndexOf("Please log in to download");
if (position > -1)
{
Debug.WriteLine("FAILED");
}
Debug.WriteLine(content);
}
}



AnswerRe: Download File From Website With Login Pin
Scott Dorman4-Oct-07 11:54
professionalScott Dorman4-Oct-07 11:54 
GeneralRe: Download File From Website With Login Pin
redfish344-Oct-07 12:24
redfish344-Oct-07 12:24 
GeneralRe: Download File From Website With Login Pin
Meysam Mahfouzi21-Sep-08 20:21
Meysam Mahfouzi21-Sep-08 20:21 
Questionparsing a string with DateTime.ParseExact Pin
dfn4-Oct-07 11:11
dfn4-Oct-07 11:11 
AnswerRe: parsing a string with DateTime.ParseExact Pin
Christian Graus4-Oct-07 11:34
protectorChristian Graus4-Oct-07 11:34 
AnswerRe: parsing a string with DateTime.ParseExact Pin
Luc Pattyn4-Oct-07 11:44
sitebuilderLuc Pattyn4-Oct-07 11:44 
AnswerRe: parsing a string with DateTime.ParseExact Pin
Scott Dorman4-Oct-07 11:51
professionalScott Dorman4-Oct-07 11:51 
AnswerRe: parsing a string with DateTime.ParseExact Pin
dfn4-Oct-07 11:59
dfn4-Oct-07 11:59 
JokeRe: parsing a string with DateTime.ParseExact Pin
Anthony Mushrow4-Oct-07 12:30
professionalAnthony Mushrow4-Oct-07 12:30 
QuestionHow to create Expander in WinForm? Pin
tantja4-Oct-07 11:04
tantja4-Oct-07 11:04 
AnswerRe: How to create Expander in WinForm? Pin
Christian Graus4-Oct-07 11:35
protectorChristian Graus4-Oct-07 11:35 
GeneralRe: How to create Expander in WinForm? Pin
tantja4-Oct-07 11:47
tantja4-Oct-07 11:47 
QuestionRe: How to create Expander in WinForm? Pin
TJoe4-Oct-07 12:14
TJoe4-Oct-07 12:14 
AnswerRe: How to create Expander in WinForm? Pin
tantja4-Oct-07 12:16
tantja4-Oct-07 12:16 
QuestionActivation or License Pin
andyr20054-Oct-07 10:25
andyr20054-Oct-07 10:25 
AnswerRe: Activation or License Pin
Ilya Verbitskiy4-Oct-07 10:48
Ilya Verbitskiy4-Oct-07 10:48 
AnswerRe: Activation or License Pin
TJoe4-Oct-07 12:11
TJoe4-Oct-07 12:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.