PayPal 验证失败并显示无效

发布于 2024-12-23 15:54:35 字数 872 浏览 0 评论 0原文

我正在尝试设置 PayPal Ipn,但某些订单验证失败。我发现如果用户名包含一些非标准字母,例如 &last_name=Montalvo Agüera ,则会失败 我需要更改编码吗?

var request = "cmd=_notify-validate&.......";

const string strLive = "https://www.paypal.com/cgi-bin/webscr";
  var req = (HttpWebRequest)WebRequest.Create(strLive);
            //Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            req.ContentLength = request.Length;

            var streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            streamOut.Write(request);
            streamOut.Close();
            var streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            var strResponse = streamIn.ReadToEnd();
            streamIn.Close();

            Response.Write(strResponse);

I am trying to set up PayPal Ipn and it is failing on some orders verification. I found that it fails if users name has some non standard letters like &last_name=Montalvo Agüera
Do I need to change encoding?

var request = "cmd=_notify-validate&.......";

const string strLive = "https://www.paypal.com/cgi-bin/webscr";
  var req = (HttpWebRequest)WebRequest.Create(strLive);
            //Set values for the request back
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            req.ContentLength = request.Length;

            var streamOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            streamOut.Write(request);
            streamOut.Close();
            var streamIn = new StreamReader(req.GetResponse().GetResponseStream());
            var strResponse = streamIn.ReadToEnd();
            streamIn.Close();

            Response.Write(strResponse);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-12-30 15:54:35

你可以这样尝试:

string strLive = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);

        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);
        strRequest += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;

        //for proxy
        //WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
        //req.Proxy = proxy;

        //Send the request to PayPal and get the response
        StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);

如果还是不行尝试将编码改为UTF8

You can try it like this:

string strLive = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);

        //Set values for the request back
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
        string strRequest = Encoding.ASCII.GetString(param);
        strRequest += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;

        //for proxy
        //WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
        //req.Proxy = proxy;

        //Send the request to PayPal and get the response
        StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);

If it still doesn't work try to change the encoding to UTF8

司马昭之心 2024-12-30 15:54:35

您需要对参数进行 url 编码。这就是 req.ContentType = "application/x-www-form-urlencoded"; 行的含义。这是您向 HTTP 服务器做出的承诺(在本例中是 PayPal 的 www.paypal.com)。我们承诺您发送的任何数据都将进行 url 编码。这意味着您必须转义任何特殊字符。这包括 ?&% 等字符以及 ü 等字符。

要对名称进行 url 编码,您需要使用 url 编码的名称构建请求:

string request = "cmd=_notify-validate&......."; // don't include "last_name="
string name = "Montalvo Agüera";
request += "last_name=" + Server.UrlEncode(name);

You need to url-encode the parameters. That's what the line req.ContentType = "application/x-www-form-urlencoded"; means. It's you making a promise to the HTTP server (in this case, PayPal's www.paypal.com). The promise is that any data you send will be url-encoded. This means you have to escape any special characters. This includes characters like ? and & and % and also characters like ü.

To url-encode the name, you need to build the request with the url-encoded name:

string request = "cmd=_notify-validate&......."; // don't include "last_name="
string name = "Montalvo Agüera";
request += "last_name=" + Server.UrlEncode(name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文