I am trying to write a program that opens a website that requires a user name and password to use. I have read many post here, on google and on MSDN and it always times out. this web server is a black box that I can't change or have access to. When I open the page with a web bowser a basic login screen pops up for user name and password so I am assuming that the following code should work but I am new to http code. I have tried the following 2 ways but it always times out. Thank you in advance,
StringBuilder sb = new StringBuilder(); String url = "<my url>"; String userName = "user"; String password = "pass"; byte[] buf = new byte[8192]; HttpWebRequest request = (HttpWebRequest) WebRequest.Creat(url); request.Method = "POST"; NetworkCredential NC = new NetworkCredential(userName, password); request.Credentials = NC; //This is where the code times out. HttpWebResponce responce = (HttpWebResponse) request.GetResponce(); Stream resStream = response.GetResponseStream(); //Then some stream print code to display raw data to console
And for the second way that I do not understand at all:
StringBuilder sb = new StringBuilder();
String url = "<my url>";
String userName = "user";
String password = "pass";
//I dont really understand how a formated string is used
String postData = string.Format("session[username]={0}&session[password]={1}", userName, password);
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest) WebRequest.Creat(url);
request.Method = "POST";
//This code makes no sense...MSDN said the options for
//contenttype are "get" or "set" but I have seen this in several
//examples
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
StreamWriter requestWriter = new StreamWriter(request.getRequestStream());
requestWriter.Write(postData);
//This is where the code times out.
HttpWebResponce responce = (HttpWebResponse) request.GetResponce();
Stream resStream = response.GetResponseStream();
//Then some stream print code to display raw data to console
I am not sure how the second one is supposed to know what field to write the username and password unless it is contained in the string format, but then how do I figure out exactly how those fields are labeled on the website? anyways thank you again for any help

New Topic/Question
Reply




MultiQuote






|