/// <summary> /// Sends a POST request. /// </summary> /// <param name="postData">A string of data to post</param> /// <param name="authentification">An IAuthentificationConfiguration. Only Basic is currently supported.</param> /// <param name="endpoint">The endpoint to send the statement to</param> /// <param name="callback">The asynchronous callback</param> /// <returns>The returned stream</returns> /// <remarks>Providing null for the callback means this is intended to be synchronous. Any exceptions will be thrown and must be handled in the main thread.</remarks> public static string PostRequest(string postData, string endpoint, IAuthenticationConfiguration authentification, TCAPI.AsyncPostCallback callback, string x_experience_api_version) { string result = null; byte[] postDataByteArray = Encoding.UTF8.GetBytes(postData); for (int attempt = 0; attempt <= REATTEMPT_COUNT; attempt++) { WebRequest request = (HttpWebRequest)WebRequest.Create(endpoint); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = postDataByteArray.Length; AddExperienceVersionHeader(request.Headers, x_experience_api_version); AddAuthHeader(request.Headers, authentification); try { result = SendRequest(request, postDataByteArray); } catch (WebException e) { if (attempt < REATTEMPT_COUNT) { try { ThrowHttpException(e); } catch (InternalServerErrorException) { Thread.Sleep(RETRY_TIMEOUT); continue; } catch (ConnectionFailedException) { Thread.Sleep(RETRY_TIMEOUT); continue; } catch (Exception) { // Suppress it for now } } if (callback == null) { // Synchronous request, let the implementer decide throw e; } else { try { ThrowHttpException(e); } catch (InternalServerErrorException) { // Connectivity Issues callback.AsyncPostConnectionFailed(e); } catch (ConnectionFailedException) { // Connectivity Issues callback.AsyncPostConnectionFailed(e); } catch (Exception) { // Permanent failure callback.AsyncPostFailure(e); } } } break; } return result; }