【问题标题】:paypal Rest Api error The request was aborted: The request was canceled. Cannot close stream until all bytes are writtenpaypal Rest Api 错误请求被中止:请求被取消。在写入所有字节之前无法关闭流
【发布时间】:2013-10-11 09:49:13
【问题描述】:

我有一个问题。 当我在我的网站项目(.net 4.0 aspx webform)上尝试使用贝宝付款(rest api & PaypalCoreSDK 版本为 1.3.4.0,RestApiSDK 版本为 0.7.3.0)时,我遇到了问题,错误消息。 但有时运行平稳 此消息是;

{"The request was aborted: The request was canceled."}
{"Cannot close stream until all bytes are written."}
   at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
   at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   at System.Net.ConnectStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.IO.StreamWriter.Dispose(Boolean disposing)
   at System.IO.TextWriter.Dispose()
   at PayPal.HttpConnection.Execute(String payLoad, HttpWebRequest httpRequest)
   at PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, Dictionary`2 headersMap, String payLoad)
   at PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, String payLoad)
   at PayPal.Api.Payments.Payment.Create(APIContext apiContext)
   at DEPOSANAT.Web.CheckOut.PaymentWithPaypal(String totalAmount, String amountCurrency, String amountDetailShipping, String amountDetailSubTotal, String amountDetailTax, String orderID, String recipientName, String shipingAddressLine1, String shipingAddressLine2, List`1 shoppinListItems) in C:\Users\tahsin.ozcelik\Desktop\DEPOSANAT_Proj_TFS\DEPOSANAT\DEPOSANAT.Web\CheckOut.aspx.cs:line 181
   at DEPOSANAT.Web.CheckOut.imgBtnPaypalOdeme_Click(Object sender, ImageClickEventArgs e) in C:\Users\tahsin.ozcelik\Desktop\DEPOSANAT_Proj_TFS\DEPOSANAT\DEPOSANAT.Web\CheckOut.aspx.cs:line 94
   at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
   at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

我的代码;

private static string GetAccessToken()
    { 
        string accessToken = new OAuthTokenCredential("XXXXXXXX", "XXXXXXXXX", GetConfig()).GetAccessToken();
        return accessToken;
    }
    public static APIContext GetAPIContext()
    {
        APIContext apiContext = new APIContext(GetAccessToken());
        apiContext.Config = GetConfig();
        return apiContext;
    }
    private static Dictionary<string, string> GetConfig()
    {
        Dictionary<string, string> configMap = new Dictionary<string, string>();
        configMap.Add("mode", "sandbox");
        configMap.Add("connectionTimeout", "36000");
       return configMap;
    }

APIContext apiContext = GetAPIContext();

        ProductLBll pLBll = new ProductLBll();
        List<Item> itms = new List<Item>();
        decimal productTotal = 0;
        foreach (var myObj in shoppinListItems)
        {
            Item myAddItem = new Item();
            myAddItem.currency = amountCurrency;
            myAddItem.name = pLBll.GetName(VariableHelper.CheckValueOfInt(myObj.ProductVariant.ProductId), SessionUser.LanguageTypeId);//pLBll.GetName(VariableHelper.CheckValueOfInt(myObj.ProductVariant.ProductId), SessionUser.LanguageTypeId);
            myAddItem.price = String.Format("{0:0.00}", myObj.ProductVariant.CurrentAmount.Value);//String.Format("{0:0.00}", myObj.ProductVariant.CurrentAmount.Value);
            myAddItem.quantity = shoppinListItems[0].Qtty.ToString();//myObj.Qtty.ToString();
            myAddItem.sku = myObj.ProductVariant.ProductId.HasValue ? myObj.ProductVariant.ProductId.Value.ToString() : "ID YOK";
            productTotal += myObj.ProductVariant.CurrentAmount.Value * myObj.Qtty;//myObj.ProductVariant.CurrentAmount.Value * myObj.Qtty;
            itms.Add(myAddItem);
        }

       ItemList itemList = new ItemList();
        itemList.items = itms;


        Payer payr = new Payer();
        payr.payment_method = "paypal";

        string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/odeme?";
        string requestUrl = Request.Url.OriginalString;
        orderID = System.Guid.NewGuid().ToString();
        string returnUrl = baseURI + "snc=true";
        //+ orderID.Substring(0, 10);
        string cancelUrl = baseURI + "snc=false";
        //+orderID.Substring(0, 13);
        RedirectUrls redirUrls = new RedirectUrls();
        redirUrls.cancel_url = cancelUrl;
        redirUrls.return_url = returnUrl;

        Details details = new Details();
        details.tax = String.Format("{0:0.00}", amountDetailTax);
       details.shipping = String.Format("{0:0.00}", amountDetailShipping);
        details.subtotal = String.Format("{0:0.00}", productTotal);



        decimal total = VariableHelper.CheckValueOfDecimal(details.tax) + VariableHelper.CheckValueOfDecimal(details.shipping) + productTotal;
        Amount amnt = new Amount();
        amnt.currency = amountCurrency;
        amnt.total = total.ToString();
        amnt.details = details;

        List<Transaction> transactionList = new List<Transaction>();
        Transaction tran = new Transaction();
        tran.description = "işlem açıklaması";
        tran.amount = amnt;
        tran.item_list = itemList;

        transactionList.Add(tran);


        Payment pymnt = new Payment();
        pymnt.intent = "sale";
        pymnt.payer = payr;
        pymnt.transactions = transactionList;
        pymnt.redirect_urls = redirUrls;
        #endregion

        string redirectUrl = "";
        try
        {
            Payment createdPayment = pymnt.Create(apiContext);
            foreach (var itemm in createdPayment.links)
            {
                if (itemm.rel.Contains("approval_url"))
                    redirectUrl = itemm.href;
            }
        }
        catch (PayPal.Exception.PayPalException ex)
        {
            //Response.Write(ex.Message);
        }

        Response.Redirect(redirectUrl);

你能快点帮我吗? 谢谢。

【问题讨论】:

    标签: c# asp.net .net paypal express-checkout


    【解决方案1】:

    我遇到了同样的问题。 对我来说,它原来是由交易描述中的某些字符和交易中的项目引起的。例如。 '欧元符号',á é ä 等等。

    似乎这个库的编码没有(正确)处理。

    【讨论】:

      【解决方案2】:

      Jan 完全正确,感谢您提供的重要线索。

      自从上周使用新的付款表单上线以来,我已经为这个偶尔出现的错误苦苦挣扎了几天。事实证明,许多国际地址都包含包含变音符号的字母 (http://en.wikipedia.org/wiki/Diacritic)。我通过在我的帖子数据中使用这些字符在贝宝沙箱中确认了这个问题,并反复得到你上面描述的确切错误。我也在使用您上面描述的相同库。

      为了尝试解决这个问题(目前在所有测试中都有效),我正在使用以下方法清理我的用户输入(地址第 1 行、第 2 行、first_name、last_name 等)

          // http://msdn.microsoft.com/en-us/library/ebza6ck1.aspx
          String NormalizeString(String value)
          {
              if (!value.IsNullOrEmpty())
              {
                  var newStringBuilder = new StringBuilder();
                  newStringBuilder.Append(value.Normalize(NormalizationForm.FormKD)
                                                  .Where(x => x < 128)
                                                  .ToArray());
                  return newStringBuilder.ToString();
              }
      
              return value;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-23
        • 2011-01-28
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        • 1970-01-01
        • 2013-05-09
        相关资源
        最近更新 更多