【问题标题】:Problems with PayPal API Http callPayPal API Http 调用问题
【发布时间】:2016-04-30 08:51:33
【问题描述】:

我已经集成了一个选项,供用户通过 PayPal 在我正在创建的网上商店中进行在线购物。当我开始收到此错误时,问题突然出现:

You must write ContentLength bytes to the request stream before calling [Begin]GetResponse. 

Http调用的代码如下:

   public string HttpCall(string NvpRequest)
        {
            string url = pEndPointURL;

            string strPost = NvpRequest + "&" + buildCredentialsNVPString();
            strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.Timeout = Timeout;
            objRequest.Method = "POST";
            objRequest.ContentLength = strPost.Length;

            try
            {
                using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
                {
                    myWriter.Write(strPost.ToString());
                }
            }
            catch (Exception e)
            {

            }

            //Retrieve the Response returned from the NVP API call to PayPal. 
            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); // this is the line where the exception occurs...
            string result;
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }

            return result;
        }

有人可以帮我解决这个问题吗?一天前它工作得很好,现在它给了我这个错误?

【问题讨论】:

    标签: c# asp.net paypal httpwebrequest httpwebresponse


    【解决方案1】:

    在浪费了几个小时之后,结果证明是 Tls 协议版本。

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    【讨论】:

      【解决方案2】:

      好的,如果有人感兴趣,我可以通过在创建 Web 请求之前添加以下行来修复错误(我可以通过像这样进入 Tls12 来修复它):

      `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12`;
      

      干杯:-)

      编辑试试这个:

       public string HttpCall(string NvpRequest)
          {
              string url = pEndPointURL;
      
              string strPost = NvpRequest + "&" + buildCredentialsNVPString();
              strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);
              ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
              // Try using Tls11 if it doesnt works for you with Tls
              HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
              objRequest.Timeout = Timeout;
              objRequest.Method = WebRequestMethods.Http.Post;
              objRequest.ContentLength = strPost.Length;
              try
              {
      
                  using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
                  {
                      myWriter.Write(strPost.ToString());
                  }
              }
              catch (Exception e)
              {
      
              }
      
              //Retrieve the Response returned from the NVP API call to PayPal. 
              HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
              string result;
              using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
              {
                  result = sr.ReadToEnd();
              }
      
              return result;
          }
      

      【讨论】:

      • 我也面临 .net 4.0 的相同问题。即使在我添加了 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;我面临同样的问题。你能帮忙吗? 4.0中没有Tls12
      • 我试图实现你的代码,但它给出了同样的错误。
      • 我的问题与所提到的不同,但您的修复对我有用。在我的情况下,PayPal 请求在本地机器上工作正常,但在生产服务器上不工作。
      【解决方案3】:
      public string HttpCall(string NvpRequest) //CallNvpServer
      {
          string url = pendpointurl;
      
          //To Add the credentials from the profile
          string strPost = NvpRequest + "&" + buildCredentialsNVPString();
          strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode( BNCode );
      
          HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
          objRequest.Timeout = Timeout;
          objRequest.Method = "POST";
          objRequest.ContentLength = strPost.Length;
      
          try
          {
              using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
              {
                  myWriter.Write(strPost);
              }
          }
          catch (Exception e)
          {
              /*
              if (log.IsFatalEnabled)
              {
                  log.Fatal(e.Message, this);
              }*/
          }
      
          //Retrieve the Response returned from the NVP API call to PayPal
      
      
          HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
          string result;
          using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
          {
              result = sr.ReadToEnd();
          }
      
          //Logging the response of the transaction
          /* if (log.IsInfoEnabled)
           {
               log.Info("Result :" +
                         " Elapsed Time : " + (DateTime.Now - startDate).Milliseconds + " ms" +
                        result);
           }
           */
          return result;
      }
      

      【讨论】:

      • 嗨,我已经更新了我的答案,复制并粘贴我的代码,如果它适合你试试看。
      猜你喜欢
      • 2013-06-16
      • 2014-07-15
      • 2014-06-26
      • 2012-05-31
      • 2018-02-17
      • 2013-09-06
      • 2012-11-21
      • 2015-01-15
      • 2013-03-18
      相关资源
      最近更新 更多