private void button4_Click(object sender, EventArgs e) { bool success = false; while (success == false) { Windows_7_Dialogs.SecurityDialog secDiag = new Windows_7_Dialogs.SecurityDialog(); secDiag.Show("Login to Notifications Application", "Please Enter in your credentials to log into the Patient Notifications Centre."); RequestObject <comdata_rqLogin> reqObj = new RequestObject <comdata_rqLogin>(); reqObj.action = "login"; reqObj.responseid = 1234; reqObj.data = new comdata_rqLogin(); reqObj.data.username = secDiag.UserData.Username; reqObj.data.password = secDiag.UserData.Password; ObjectWrapper <RequestObject <comdata_rqLogin>, ResponseObject <comdata_rtLogin> > objWrap = new ObjectWrapper <RequestObject <comdata_rqLogin>, ResponseObject <comdata_rtLogin> >(reqObj); //objWrap.submitQuery(); String JSON = ObjectConverter.ToJSON <RequestObject <comdata_rqLogin> >(reqObj); String rtJSON = SrvCon.Connection.SendData(JSON); ResponseObject <comdata_rtLogin> rtObject = ObjectConverter.ToObject <ResponseObject <comdata_rtLogin> >(rtJSON); if (rtObject.error_code == 0) { if (rtObject.data.loginSuccessful) { success = true; MessageBox.Show("Login Successful! Welcome: " + reqObj.data.username); } else { MessageBox.Show("Login Unsuccessful"); } } else { MessageBox.Show("Login Unsuccessful"); //Error has happened } } }
/// <summary> /// Request a login from the SMSGlobal Soap API, get a ticket in return and put it into apiSendSMS class. /// </summary> /// <param name="_vlogin">ApiValidateLogin object</param> /// <param name="_sendSMS">apiSendSMS object</param> /// public void requestLogin(apiValidateLogin _vlogin, apiSendSms _sendSMS) { this.apiv = _vlogin; apiSendSms apis = _sendSMS; string loginTicket = null; HttpWebRequest request = WebRequest.Create("http://www.smsglobal.com/mobileworks/soapserver.php") as HttpWebRequest; Windows_7_Dialogs.SecurityDialog a = new Windows_7_Dialogs.SecurityDialog(); /// Get proxy information if needed. //if (!request.Proxy.IsBypassed(request.RequestUri)) if (isProxyActive(request.RequestUri, request) == true) { a.Show("Proxy Authentication", "The server you are trying to access requires a username and password." + Environment.NewLine); _cred = new NetworkCredential(a.UserData.Username, a.UserData.Password); request.Proxy.Credentials = _cred; } /// Add the needed headers for the SOAP API. request.Method = "POST"; request.ContentType = "text/xml"; request.Headers.Add("urn:MobileWorks#apiValidateLogin"); ///Create an XML Document, with the needed data inside. var document = new XDocument( new XDeclaration("1.0", String.Empty, String.Empty), new XElement(soapenv + "Envelope", new XAttribute(XNamespace.Xmlns + "SOAP-ENV", soapenv), new XElement(soapenv + "Body", new XElement("apiValidateLogin", new XElement("user", apiv.APIusername), new XElement("password", apiv.APIpassword) )))); document.Declaration.Version = "1.0"; ///As it doesn't seem to want to make the declaration, a work around is used. Creating a file, then appending the xml document made above, then loaded into the ///document object. //if (File.Exists(Environment.CurrentDirectory + @"\apiLogin.xml")) File.Delete(Environment.CurrentDirectory + @"\apiLogin.xml"); //File.WriteAllText(Environment.CurrentDirectory + @"\apiLogin.xml", "<?xml version='1.0' ?>" + Environment.NewLine); //File.AppendAllText(Environment.CurrentDirectory + @"\apiLogin.xml", document.ToString()); //document = XDocument.Load(Environment.CurrentDirectory + @"\apiLogin.xml"); ///Write the document to the requested place, in this case is the Soap API at SMSGLobal.com var writer = new StreamWriter(request.GetRequestStream()); writer.WriteLine(document); writer.Close(); ///Get the responce from the webserver after writing to the API. using (var rsp = request.GetResponse()) { request.GetRequestStream().Close(); if (rsp != null) { using (var answerReader = new StreamReader(rsp.GetResponseStream())) { ///Get the ticket which the server sent back using Regex to get the letter/digit mix of 32 characters. var readString = answerReader.ReadToEnd(); Regex r = new Regex(@"(.*)ticket>(.*)</ticket(.*)"); if (r.IsMatch(readString.ToString())) { Regex reg = new Regex(@"[A-Za-z0-9]{32}"); loginTicket = reg.Match(r.Match(readString.ToString()).ToString()).ToString(); } } } } apis.ticket = loginTicket; }