简体   繁体   English

PayPal API Http调用问题

[英]Problems with PayPal API Http call

I've integrated an option for users to pay via PayPal their online shopping on the web shop that I'm creating. 我已经为用户集成了一个选项,通过PayPal在我正在创建的网上商店进行在线购物。 The problem came up suddenly when I started to get this error: 当我开始收到此错误时突然出现问题:

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

And the code for the Http call is as following: 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;
        }

Can someone help me out with this? 有人可以帮我解决这个问题吗? It worked fine a day ago, now its giving me this error? 它一天前工作正常,现在它给我这个错误?

Okay so if anyone is interested, I was able to fix the error by adding the following line before creating the web request (I was able to fix it by going down to Tls12 like this): 好的,如果有人有兴趣,我可以通过在创建Web请求之前添加以下行来修复错误(我能够通过像这样下载到Tls12来修复它):

`ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12`;

Cheers :-) 干杯:-)

Edit try this: 编辑试试这个:

 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM