PayPal验证失败,显示Invalid

本文关键字:显示 Invalid 失败 验证 PayPal | 更新日期: 2023-09-27 18:19:29

我正在尝试设置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);

PayPal验证失败,显示Invalid

您可以这样尝试:

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

您需要对参数进行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);