Michael Maddison
michael at ddisolutions.com.au
Wed Jun 16 18:34:04 CDT 2010
Hi John, If I read your request correctly you want to send/receive data to a web page/s from a program. If so I did this recently in a proof of concept and go it working. My target site was a online betting site. I used C# and a program called Fiddler (free) to read the actual POSTS and RESPONSEs and built my own in code to emulate the original including logging in and retrieving data like balances and placing bets. Heres my login code based on samples from google, I'm sure it could be written better but it did work... Cheers Michael private bool LoginToTAB ( ) { string START_URL = "http://www.tab.com.au/ExpressBet/Standard/Default.aspx?State=2"; string LOGIN_URL = "http://www.tab.com.au/ExpressBet/LoginFrame.aspx?State=2&ExpressBetMode =Standard"; string SECRET_PAGE_URL = "https://www.tab.com.au/Login/LoginUser.aspx?State=2&ReturnURL=http://ww w.tab.com.au/ExpressBet/LoginFrame.aspx?State=2&ExpressBetMode=Standard" ; #region -- GET Start Page -- HttpWebRequest startPageWebRequest = (HttpWebRequest) WebRequest.Create ( START_URL ); startPageWebRequest.CookieContainer = new CookieContainer ( ); HttpWebResponse startPageWebResponse = (HttpWebResponse) startPageWebRequest.GetResponse ( ); //Get cookies for later use startPageWebResponse.Cookies = startPageWebRequest.CookieContainer.GetCookies ( startPageWebRequest.RequestUri ); _cc = startPageWebRequest.CookieContainer; #endregion #region -- GET Login Page -- startPageWebRequest = (HttpWebRequest) WebRequest.Create ( LOGIN_URL ); startPageWebRequest.CookieContainer = new CookieContainer ( ); startPageWebResponse = (HttpWebResponse) startPageWebRequest.GetResponse ( ); //loginWebResponse.Cookies = loginWebRequest.CookieContainer.GetCookies ( loginWebRequest.RequestUri ); //foreach ( Cookie cky in loginWebResponse.Cookies ) //{ // if ( cky.Name == "ASP.NET_SessionId" ) // { // cook = cky; // break; // } //} Stream strm = startPageWebResponse.GetResponseStream ( ); StreamReader sr = new StreamReader ( strm ); string s = string.Empty; while ( sr.Peek ( ) > -1 ) { s += sr.ReadLine ( ); } sr.Close ( ); startPageWebResponse.Close ( ); #endregion //Console.WriteLine ( s ); _viewState = ExtractViewState ( s ); _eventValidation = ExtractEventValidation ( s ); _prevPage = ExtractPreviousPage ( s ); //_sessionID = cook.Value; // ExtractSessionID ( s ); _postData = String.Format ( "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}&DisplayExpressTopNav1$T extboxLogin={1}&DisplayExpressTopNav1$TextboxPassword={2}" + "&DisplayExpressTopNav1$ShowBetSlipOnLogin={3}&__PREVIOUSPAGE={4}&__EVEN TVALIDATION={5}", _viewState, Username, Password, "false", _prevPage, _eventValidation ); #region -- POST Login -- HttpWebRequest loginWebRequest = (HttpWebRequest) WebRequest.Create ( SECRET_PAGE_URL ); loginWebRequest.CookieContainer = new CookieContainer ( ); loginWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; // "*/*"; loginWebRequest.Referer = "http://www.tab.com.au/ExpressBet/LoginFrame.aspx?State=2&ExpressBetMode =Standard"; loginWebRequest.ContentLength = _postData.Length; loginWebRequest.ContentType = "application/x-www-form-urlencoded"; loginWebRequest.Method = "POST"; loginWebRequest.KeepAlive = true; loginWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 (.NET CLR 3.5.30729)"; foreach ( Cookie cky in startPageWebResponse.Cookies ) { loginWebRequest.CookieContainer.Add ( cky ); //Console.WriteLine ( "hwr2 - " + cky.Name + " = " + cky.Value ); } string strResponse = string.Empty; // Write the request using ( StreamWriter stOut = new StreamWriter ( loginWebRequest.GetRequestStream ( ), System.Text.Encoding.ASCII ) ) { stOut.Write ( _postData ); } // Do the request to get the response using ( StreamReader stIn = new StreamReader ( loginWebRequest.GetResponse ( ).GetResponseStream ( ) ) ) { strResponse = stIn.ReadToEnd ( ); } HttpWebResponse loginWebResponse = (HttpWebResponse) loginWebRequest.GetResponse ( ); loginWebResponse.Cookies = loginWebRequest.CookieContainer.GetCookies ( startPageWebRequest.RequestUri ); //foreach ( Cookie cky in loginWebResponse.Cookies ) //{ // //cook = cky; // Console.WriteLine ( "Login - " + cky.Name + " = " + cky.Value ); //} #endregion _cc = loginWebRequest.CookieContainer; return strResponse.Contains ( Username ); } I have a client that has to do data entry into an internet screen. I am researching whether a program can manipulate an internet form, poke values into specific controls, click buttons etc. I have looked at the source for one specific screen and it appears to be all java. For example, top of the screen <html> <head> <title>Professional Claim</title> <link href='/ePROM/CSS/styles.css' rel='stylesheet' type='text/css'> <script src='/ePROM/ScriptLibrary/utils.js'></script> <script src='/ePROM/ScriptLibrary/soap.js'></script> <script src='/ePROM/ScriptLibrary/gui.js'></script> <script src='/ePROM/ScriptLibrary/xsd.js'></script> <script src='/ePROM/ScriptLibrary/xbind.js'></script> <script src='/ePROM/ScriptLibrary/validation.js'></script> <script src='/ePROM/ScriptLibrary/calendar.js'></script> <script src='/ePROM/ScriptLibrary/techSupport.js'></script> <script language=javascript> var g_fFieldsChanged = 0; var hrs = 0; // DF 5462 var mins = 90; // DF 5462 var secs = 0; // DF 5462 </script> <SCRIPT FOR=window EVENT=onload> // Heir Menus if (typeof(HM_f_StartIt) != "undefined") HM_f_StartIt(); // Call any user defined "onload" routines. var inx; ######################################################################## ########### So... do developers poke data into screens, and read values back from screens, click buttons, select menu items etc? I did this kind of thing back in the late 90s but so much has changed. I am just beginning to investigate whether this is possible and where to go to learn how. Any help appreciated -- John W. Colby www.ColbyConsulting.com _______________________________________________ dba-VB mailing list dba-VB at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-vb http://www.databaseadvisors.com No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.829 / Virus Database: 271.1.1/2928 - Release Date: 06/17/10 04:35:00