c# - 尝试结帐时 PayPal 功能中的 GetResponse() 错误

标签 c# .net paypal

所以我正在关注 WingTip Toy tutorial ,我知道它有点旧,但它没有错误,直到我需要使用沙盒开发工具使用 PayPal 结帐

这是发生错误的代码

//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;

这是我运行时遇到的错误

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

请注意我是初学者

编辑:完整代码在这里

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);
            }
        }
        catch (Exception)
        {
            // No logging for this tutorial.
        }

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

最佳答案

objRequest.ContentLength = strPost.Length;

你想在这里做什么?框架自动设置内容长度。内容长度以字节为单位,但您已给出字符数。

这就是错误提示的原因:你写的字符数与你说的不同。

删除该行。

如果您使用HttpClient,您的代码会变得简单得多。应该大约 5 行。

关于c# - 尝试结帐时 PayPal 功能中的 GetResponse() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37430752/

相关文章:

c# - 从datagridview中删除数据库

c# - ASP.NET 模板

c# - 您应该混淆商业 .Net 应用程序吗?

c# - 当应用程序继续运行时,如何清理 .NET 中的 COM 引用?

ruby-on-rails - 在活跃商户应用程序中使用两步验证的卡(信用卡/借记卡)会发生什么情况?

php - 自适应支付 paypal - 支付 api 操作没有 pdt 数据

C# 记录 SMB 文件访问

c# - QuickFIX 对手方 DataDictionary 给出无效类型异常

.net - 是否可以使用 Monodevelop 构建可移植类库?

php - Magento 中的 Paypal 状态