[英]PayPal verify fail with Invalid
我正在尝试设置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);
您需要对参数进行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);
您可以这样尝试:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.