2012-01-03 307 views
1

我试图设置PayPal Ipn,它在某些订单验证上失败。我发现它失败如果用户名称有一些非标准字母,如&last_name=Montalvo Agüera 我是否需要更改编码?PayPal验证失败,无效

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

回答

0

您需要对参数进行网址编码。这就是req.ContentType = "application/x-www-form-urlencoded";的含义。这是你向HTTP服务器作出承诺(在这种情况下,贝宝的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); 
+0

不,这不起作用。 – Tomas 2012-01-03 09:07:13

+0

仍然无效。两天试图解决问题没有取得任何成功。我从“即时付款通知(IPN)详细信息”页面获得IPN消息,其中一些失败。我甚至认为只是删除验证。 – Tomas 2012-01-03 09:17:26

+0

你能编辑这个问题吗?它有两个示例请求 - 一个可以工作,一个不可以?可能还有其他问题。 – scraimer 2012-01-03 10:48:15

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

如果仍然没有按” t工作尝试将编码更改为UTF8

+0

我得到同样的问题。某些订单的状态无效。 – Tomas 2012-01-03 09:08:13

+0

如果我将其更改为UTF8,则会出现错误“在写入所有字节之前无法关闭流。” – Tomas 2012-01-03 09:18:47

+0

我认为你应该联系贝宝 – kleinohad 2012-01-03 09:36:48