///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Sends an HTTP Request to the backend and parse the response (expected as a BaseEntity object) /// </summary> /// <param name="action">action to query (e.g. "Snippets/Get")</param> /// <param name="data"> /// if the request is a GET, the parameters to be put in the querystring (e.g. "snippetID=100"), /// otherwise data to be put in the post (e.g. "content=...") /// </param> /// <param name="isPost">whether this request is a GET(false) or a POST(true)</param> /// <param name="requiresLogin">false if this request calls a public web service</param> /// <returns>null if any error occurred; the result of the invocation otherwise</returns> protected S2CResBaseEntity <T> SendReqBaseEntity <T>(string action, string data, bool isPost, bool requiresLogin = true) where T : BaseEntity, new() { string response = PrepareAndSendReq(action, data, isPost, requiresLogin); if (string.IsNullOrEmpty(response)) { ErrorCodes errCode = ErrorCodes.COMMUNICATION_ERROR; if (WebConnector.Current.IsTimeout) { errCode = ErrorCodes.TIMEOUT; } SetLastError(log, errCode, S2CRes <string> .GetErrorMsg(errCode)); return(new S2CResBaseEntity <T>(0.0, errCode, null)); } S2CResBaseEntity <T> resp = S2CSerializer.DeserializeBaseEntity <T>(response, m_serialFormat); if (!CheckResp <T>(resp)) { PrintRespError <T>(resp); //if the problem is related to user not logged in, reset login status and retry another time: if (requiresLogin && (resp != null) && (resp.Status == ErrorCodes.NOT_LOGGED_IN)) { WebConnector.Current.ResetLoginStatus(); //reset login status //retry the WS call: response = PrepareAndSendReq(action, data, isPost, requiresLogin); resp = S2CSerializer.DeserializeBaseEntity <T>(response, m_serialFormat); } } return(resp); }
/// <summary> /// Checks the given response and returns the expected BaseEntity object, if any /// </summary> /// <typeparam name="T"></typeparam> /// <param name="resp">null if any error occurred, the expected object otherwise</param> /// <returns></returns> protected T ParseBaseEntityResponse <T>(S2CResBaseEntity <T> resp) where T : BaseEntity { //build the result: if (!CheckResp(resp)) { PrintRespError(resp); return(null); } return((T)resp.Data.ToBaseEntity()); }
public SnippetComment GetComment(long commentID) { //check for erroneous input: if (commentID <= 0) { return(null); } //send the request and parse the response: S2CResBaseEntity <SnippetCommentComm> resp = SendReqBaseEntity <SnippetCommentComm>(GET_COMMENT_URL, "commentID=" + commentID, false); //build the result: return(ParseBaseEntityResponse <SnippetCommentComm>(resp)); }
public Snippet GetSnippetByName(string name) { //check for erroneous input: if (string.IsNullOrWhiteSpace(name)) { SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}, name={1}", CurrentUserID, name.PrintNull())); return(null); } //send the request and parse the response: S2CResBaseEntity <SnippetComm> resp = SendReqBaseEntity <SnippetComm>(GET_BYNAME_URL, HttpUtility.UrlEncode(name), false, false); //build the result: return(ParseBaseEntityResponse <SnippetComm>(resp)); }
public Snippet GetSnippetByID(long snippetID) { //check for erroneous input: if (snippetID <= 0) { return(null); } //send the request and parse the response: S2CResBaseEntity <SnippetComm> resp = SendReqBaseEntity <SnippetComm>(GET_SNIPPET_URL, "snippetID=" + snippetID, false, false); log.DebugFormat("Snippet {0} taken in {1} ms", snippetID, (resp == null ? -1 : resp.ExecTime)); //build the result: return(ParseBaseEntityResponse <SnippetComm>(resp)); }
/// <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> /// 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; }