private void GetRequest(string requestUri, AsyncPostCallback callback) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri); request.ContentType = "application/x-www-form-urlencoded"; request.BeginGetResponse(new AsyncCallback((callbackResult) => { try { HttpWebRequest request2 = (HttpWebRequest)callbackResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request2.EndGetResponse(callbackResult); string responseString = ""; Stream streamResponse = response.GetResponseStream(); StreamReader reader = new StreamReader(streamResponse); responseString = reader.ReadToEnd(); streamResponse.Close(); reader.Close(); response.Close(); callback(responseString); } catch (Exception e) { callback(null); } }), request); }
public static void PostRequest(string requestUri, string parameters, AsyncPostCallback callback) { HttpWebRequest spAuthReq = HttpWebRequest.Create(requestUri) as HttpWebRequest; spAuthReq.ContentType = "application/x-www-form-urlencoded"; spAuthReq.Method = "POST"; spAuthReq.BeginGetRequestStream(new AsyncCallback((callbackResult) => { HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState; Stream postStream = myRequest.EndGetRequestStream(callbackResult); byte[] byteArray = Encoding.UTF8.GetBytes(parameters); postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); myRequest.BeginGetResponse(new AsyncCallback((callbackResult2) => { try { HttpWebRequest request = (HttpWebRequest)callbackResult2.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult2); string responseString = ""; Stream streamResponse = response.GetResponseStream(); StreamReader reader = new StreamReader(streamResponse); responseString = reader.ReadToEnd(); streamResponse.Close(); reader.Close(); response.Close(); callback(responseString); } catch (Exception e) { callback(null); } }), myRequest); }), spAuthReq); }
/// <summary> /// Construct a TCAPI object with asynchronous support. /// </summary> /// <param name="endpoint">The LRS endpoint</param> /// <param name="authentification">Authentification object</param> /// <param name="tcapiCallback">Asynchornous callback object</param> /// <param name="offlineStorage">Offline Storage object</param> /// <param name="statementPostInterval">Interval for asynchronous operations to take place, in milliseconds</param> public TCAPI(Uri endpoint, IAuthenticationConfiguration authentification, ITCAPICallback tcapiCallback, IOfflineStorage offlineStorage, int statementPostInterval, int maxBatchSize) { this.endpoint = endpoint; this.authentification = authentification; this.tcapiCallback = tcapiCallback; this.offlineStorage = offlineStorage; this.statementPostInterval = statementPostInterval; this.maxBatchSize = maxBatchSize; this.asyncPostCallback = new AsyncPostCallback(this.PostSuccess, this.PostFailed, this.PostConnectionFailed); this.isAsyncFlushing = false; asyncPostTimer = new Timer(); asyncPostTimer.Elapsed += new ElapsedEventHandler(AsyncPostTimerElapsed); asyncPostTimer.Interval = this.statementPostInterval; asyncPostTimer.Enabled = this.statementPostInterval > 0; asyncPostTimer.AutoReset = true; this.version = DetermineVersioning(); }
/// <summary> /// Stores statements synchronously, must be called from a separate thread /// </summary> /// <param name="statements">Statements to store</param> /// <param name="callback">Callback to signal upon completion</param> private void StoreStatements(Statement[] statements, AsyncPostCallback callback) { // Break the statement into a 2D array. First index is the number of batches, second is the number of // statements in that batch, which is either maxBatchSize or statements.Length % maxBatchSize Statement[][] batches = new Statement[(statements.Length - 1) / maxBatchSize + 1][]; for (int i = 0; i < batches.Length; i++) { batches[i] = new Statement[statements.Length - (maxBatchSize * (i + 1)) >= 0 ? maxBatchSize : statements.Length % maxBatchSize]; Array.Copy(statements, i * maxBatchSize, batches[i], 0, batches[i].Length); } for (int round = 0; round < batches.Length; round++) { foreach (Statement s in batches[round]) { var failures = new List<ValidationFailure>(s.Validate(earlyReturnOnFailure: true)); if (failures.Count > 0) { throw new ArgumentException(failures[0].Error, "statements"); } } TinCanJsonConverter converter = new TinCanJsonConverter(); string postData; switch (version) { case TCAPIVersion.TinCan090: Model.TinCan090.Statement[] currentBatch = new RusticiSoftware.TinCanAPILibrary.Model.TinCan090.Statement[batches[round].Length]; for (int i = 0; i < currentBatch.Length; i++) currentBatch[i] = (Model.TinCan090.Statement)batches[round][i]; postData = converter.SerializeToJSON(currentBatch); break; default: postData = converter.SerializeToJSON(batches[round]); break; } HttpMethods.PostRequest(postData, endpoint + STATEMENTS, authentification, callback, versionString); } }