PayPal API Http 调用的问题

本文关键字:问题 调用 Http API PayPal | 更新日期: 2023-09-27 18:31:32

我已经集成了一个选项,供用户通过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;
        }

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

PayPal API Http 调用的问题

好的,如果有人感兴趣,我可以通过在创建 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;
    }
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;
}

浪费了几个小时后,原来是TLS协议版本。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;