PayPal API Http 调用问题

Posted

技术标签:

【中文标题】PayPal API Http 调用问题【英文标题】:Problems with PayPal API Http call 【发布时间】: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;
        

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

【问题讨论】:

【参考方案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;

【讨论】:

嗨,我已经更新了我的答案,复制并粘贴我的代码,如果它适合你试试看。

以上是关于PayPal API Http 调用问题的主要内容,如果未能解决你的问题,请参考以下文章

Paypal自适应退款

PayPal API Http 调用问题

PayPal 的定期订阅按钮与其他计划的优缺点是啥

Woocommerce Paypal REST api 与 React JS 的集成

使用 Paypal Adaptive Payments 设置标题图片

如何在 laravel 中集成 Paypal 支付网关?