【问题标题】:GetResponse() Error in the PayPal functionality when trying to checkout尝试结帐时 PayPal 功能中的 GetResponse() 错误
【发布时间】:2016-09-22 16:20:43
【问题描述】:

所以我关注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;
    }

【问题讨论】:

  • 你能显示你写请求正文的部分吗?
  • @peter 将其添加到问题中

标签: c# .net paypal


【解决方案1】:
objRequest.ContentLength = strPost.Length;

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

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

删除该行。

如果您使用HttpClient,您的代码会变得简单很多。应该是 5 行左右。

【讨论】:

  • 我评论了那一行,我构建并运行了应用程序,我得到了同样的错误,并且在这一行 HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
  • 请发布完整的更新代码和异常 ToString 输出。
  • 实际上错误更改为无法创建 SSL/TLS 安全通道。我会去最初的 Microsoft cmets 我认为他们发布了一些关于此的内容,评论它可能已经解决了这个问题!
猜你喜欢
  • 2021-04-21
  • 2020-04-09
  • 2017-03-21
  • 2016-11-13
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
相关资源
最近更新 更多