PayPal API Http 调用的问题 [英] Problems with PayPal API Http call

查看:20
本文介绍了PayPal API Http 调用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've integrated an option for users to pay via PayPal their online shopping on the web shop that I'm creating. 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. 

Http调用的代码如下:

And the code for the Http call is as following:

   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?

推荐答案

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

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):

`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;
    }

这篇关于PayPal API Http 调用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆