/// <summary> /// Detects whether the Snip2Code web service is alive or not /// </summary> /// <returns></returns> public bool PingS2CServer(string pingUrl) { bool res = false; using (S2cWebClient client = new S2cWebClient(10000, this.m_requestFormat)) { try { string retStr = client.DownloadString(pingUrl); S2CResObj <object> boolResp = S2CSerializer.DeserializeObj(retStr, this.m_requestFormat); if ((boolResp != null) && (boolResp.Status == ErrorCodes.OK)) { if (!bool.TryParse(boolResp.Data.ToString(), out res)) { res = false; } } } catch (Exception e) { //log.DebugFormat("Cannot connect due to {0}; response is {1}", e, (response == null ? "null" : response.StatusCode.ToString())); log.DebugFormat("Cannot connect due to {0}", e); } } log.Debug("CheckConnections result=" + res); return(res); }
/// <summary> /// Login into the web server using OneAll system in order to be ready to send web service requests /// </summary> /// <param name="identToken"></param> public UserClient LoginWithOneAll(string identToken) { if (identToken.IsNullOrWhiteSpaceOrEOF()) { return(new UserClient(false)); } string postData = "identToken=" + identToken; // now post to the login form #if USE_OAUTH //check the validity of Access Token: if (AccessTokenInvalid()) { //maybe in the future we can also refresh the token instead of creating a new one after the expiration... if (!ReceiveAccessToken(User.ONEALL_FAKE_USERNAME, identToken)) { return(new UserClient(false)); } } string loginUrl = string.Format("{0}{1}?{2}", BaseWS.Server, OAUTH_ONEALL_LOGIN_URL, postData); string response = SendGetRequest(loginUrl, true); #else string loginUrl = string.Format("{0}{1}", BaseWS.Server, LOGIN_ONEALL_URL); string response = SendPostRequest(loginUrl, postData, false); #endif S2CResBaseEntity <UserClient> resp = null; if (string.IsNullOrEmpty(response)) { resp = new S2CResBaseEntity <UserClient>(-1, ErrorCodes.COMMUNICATION_ERROR, null); } else { resp = S2CSerializer.DeserializeBaseEntity <UserClient>(response, m_requestFormat); } //check that the authentication request has been correctly received: m_isLogged = ((m_cookies != null) && (m_cookies.Count > 0) && (resp != null) && (resp.Status == ErrorCodes.OK) && (resp.Data != null)); if ((resp != null) && (resp.Status == ErrorCodes.FAIL)) { return(new UserClient(false)); } if (resp == null) { return(null); } else { return(resp.Data); } }
/// <summary> /// Perform a logout operation on the server for the current user /// </summary> /// <returns></returns> public bool Logout() { string logoutUrl = string.Format("{0}{1}", BaseWS.Server, LOGOUT_URL); string response = SendGetRequest(logoutUrl, true); if (string.IsNullOrEmpty(response)) { return(false); } S2CResObj <object> resp = S2CSerializer.DeserializeObj(response, m_requestFormat); if ((resp != null) && (resp.Status == ErrorCodes.OK)) { ResetLoginStatus(); return(true); } return(false); }
/// <summary> /// Login into the web server in order to be ready to send web service requests /// </summary> /// <param name="username"></param> /// <param name="password"></param> public UserClient Login(string username, string password) { string viewStateString = string.Empty; #if NEED_VIEW_STATE // first, request the login form to get the viewstate value HttpWebRequest viewStateRequest = WebRequest.Create(loginUrl) as HttpWebRequest; viewStateRequest.Accept = "application/xhtml+xml"; StreamReader responseReader = new StreamReader(viewStateRequest.GetResponse().GetResponseStream()); string responseData = responseReader.ReadToEnd(); responseReader.Close(); // extract the viewstate value and build out POST data string viewState = ExtractViewState(responseData); if (viewState != string.Empty) { viewStateString = string.Format("__VIEWSTATE={0}&", viewState); } #endif string encryptedPsw = password.ComputeMD5HashSQLServerCompliant(); string postData = string.Format("{0}{1}={2}&{3}={4}&{5}={6}", viewStateString, USERNAME_TEXTBOX_NAME, HttpUtility.UrlEncode(username), PASSWORD_TEXTBOX_NAME, HttpUtility.UrlEncode(encryptedPsw), LOGIN_BUTTON_NAME, LOGIN_BUTTON_VALUE); // now post to the login form #if USE_OAUTH //check the validity of Access Token: if (AccessTokenInvalid()) { //maybe in the future we can also refresh the token instead of creating a new one after the expiration... if (!ReceiveAccessToken(username, encryptedPsw)) { return(new UserClient(false)); } } string loginUrl = string.Format("{0}{1}?{2}", BaseWS.Server, OAUTH_LOGIN_URL, postData); string response = SendGetRequest(loginUrl, true); #else string loginUrl = string.Format("{0}{1}", BaseWS.Server, LOGIN_URL); string response = SendPostRequest(loginUrl, postData, false); #endif S2CResBaseEntity <UserClient> resp = null; if (string.IsNullOrEmpty(response)) { resp = new S2CResBaseEntity <UserClient>(-1, ErrorCodes.COMMUNICATION_ERROR, null); } else { resp = S2CSerializer.DeserializeBaseEntity <UserClient>(response, m_requestFormat); } //check that the authentication request has been correctly received: m_isLogged = ((m_cookies != null) && (m_cookies.Count > 0) && (resp != null) && (resp.Status == ErrorCodes.OK) && (resp.Data != null)); if ((resp != null) && (resp.Status == ErrorCodes.FAIL)) { return(new UserClient(false)); } if (resp == null) { return(null); } else { return(resp.Data); } }