public string HttpUpload(string data, bool tryRefreshingOAuthAccessToken = true)
        {
            var httpClient = GetAuthorizedHttpClient();

            if (OAuthSpecified())
            {
                try
                {
                    var response = httpClient.PostAsync(_transactHttpsUrl, new StringContent(data)).Result;
                    return(response.Content.ReadAsStringAsync().Result);
                }
                catch (WebException ex)
                {
                    var response = ex.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        _accessTokenProvider.Refresh();
                        return(HttpUpload(data, tryRefreshingOAuthAccessToken: false));
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else
            {
                var response = httpClient.PostAsync(_transactHttpsUrl, new StringContent(data)).Result;
                return(response.Content.ReadAsStringAsync().Result);
            }
        }
Example #2
0
        public string HttpUpload(string data, bool tryRefreshingOAuthAccessToken = true, bool useXMLAPI = false, string XMLAPISession = "")
        {
            var    httpClient = GetAuthorizedHttpClient();
            string httpsURL   = useXMLAPI ? _XMLAPIHttpsUrl + XMLAPISession : _transactHttpsUrl;
            string mediaType  = useXMLAPI ? "text/xml" : "application/x-www-form-urlencoded";
            var    content    = useXMLAPI ? new StringContent(data, Encoding.UTF8, mediaType) : new StringContent(data);

            if (OAuthSpecified())
            {
                try
                {
                    var response = httpClient.PostAsync(httpsURL, content)
                                   .ConfigureAwait(false).GetAwaiter().GetResult();
                    return(response.Content.ReadAsStringAsync()
                           .ConfigureAwait(false).GetAwaiter().GetResult());
                }
                catch (WebException ex)
                {
                    if (ex.Response is HttpWebResponse response && response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        _accessTokenProvider.Refresh();
                        return(HttpUpload(data, tryRefreshingOAuthAccessToken: false));
                    }
                    else
                    {
                        throw;
                    }
                }