/// <summary> /// オンラインに非同期でセーブコマンドを送信するメソッド /// </summary> /// <param name="data">RankigData型:送信するランキングデータ</param> /// <returns>実行したSQLコマンド, もしくはサーバーから返信されたエラーメッセージ</returns> private async Task <string> SendOnlineSaveData(RankingData data) { var content = new System.Net.Http.FormUrlEncodedContent(data.Dictionary()); var client = new System.Net.Http.HttpClient(); Log.Info("【Online】Access to server."); Log.Info($"【Online】Address is [{BaseUrl}{SAVE_DATA_URL}]."); Log.Info($"【Online】【Transmission】Ranking Data [{data.ToString()}]."); Log.Info($"【Online】【Transmission】Contents [{ content.ToString()}]."); var response = await client.PostAsync(BaseUrl + SAVE_DATA_URL, content); return(await response.Content.ReadAsStringAsync()); }
/// <summary> /// The save credit card info. /// </summary> /// <param name="creditCardInfo">The credit card info.</param> /// <param name="userId">the user id.</param> /// <returns> /// The <see> /// <cref>AsyncCallResult</cref> /// </see> /// . /// </returns> public AsyncCallResult<string> SaveCreditCardInfo(CreditCardInfo creditCardInfo, string userId) { var source = new CancellationTokenSource(); CancellationToken token = source.Token; string response = "Connection Timeout"; TransactionTypes transactionType = TransactionTypes.AddCreditCard; using ( var contents = new FormUrlEncodedContent( new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>( "redirect_url", this.redirectURI), new KeyValuePair<string, string>( "api_login", this.securityKeys.Credentials.UserName), new KeyValuePair<string, string>( "credit_card[number]", creditCardInfo.CreditCardNumber), new KeyValuePair<string, string>( "credit_card[month]", creditCardInfo.CreditCardMonth.ToString()), new KeyValuePair<string, string>( "credit_card[year]", creditCardInfo.CreditCardYear.ToString()), new KeyValuePair<string, string>( "credit_card[verification_value]", creditCardInfo.CrediCardCVV.ToString()), new KeyValuePair<string, string>( "credit_card[first_name]", creditCardInfo.FirstName), new KeyValuePair<string, string>( "credit_card[last_name]", creditCardInfo.LastName) })) using (var handler = new HttpClientHandler { AllowAutoRedirect = false, PreAuthenticate = false }) using (var client = new HttpClient(handler)) { const string URI = "https://spreedlycore.com/v1/payment_methods"; string request = contents.ToString(); using (Task<HttpResponseMessage> task = client.PostAsync(URI, contents, token)) { try { if (task.Wait(20000, token) == false) { if (token.CanBeCanceled) { source.Cancel(); } this.LogToDb(userId, request, response, transactionType); return new AsyncCallResult<string>(AsyncCallFailureReason.TimeOut); } } catch (Exception ex) { request = "Connection Failure, error : - " + ex; this.LogToDb(userId, request, response, transactionType); return new AsyncCallResult<string>(AsyncCallFailureReason.FailedConnection); } if (task.Result.StatusCode != HttpStatusCode.Redirect) { request = "Connection Failure"; this.LogToDb(userId, request, response, transactionType); return new AsyncCallResult<string>( AsyncCallFailureReason.FailedStatusCode, task.Result.Content.ReadAsStringAsync().Result); } response = task.Result.Content.ToString(); this.LogToDb(userId, request, response, transactionType); return new AsyncCallResult<string>( AsyncCallFailureReason.None, task.Result.Headers.Location.Query.Substring(1)); } } }