PayPal验证失败,无效

时间:2012-01-03 08:59:16

标签: c# paypal paypal-ipn

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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

你可以这样试试:

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