我有以下C#ASP.Net代码,用于在购买后处理PayPal IPN。我已将PayPal设置中的编码设置为UTF8。当我使用ASCII编码将请求发送回PayPal时(代码中的所有UTF8都替换为ASCII)一切正常。当我使用UTF8编码发送请求时,我收到“请求已中止:请求已取消。”最后一行的streamOut.Close()异常。我正在使用Godaddy共享主机与IIS 7和.Net 2启用。有什么建议吗?
protected void Page_Load(object sender, EventArgs e)
{
string strRequest = string.Empty;
try
{
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLive);
req.KeepAlive = false;
req.ReadWriteTimeout = 600000;
req.Timeout = 600000;
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
strRequest = Encoding.UTF8.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = System.Text.Encoding.UTF8.GetByteCount(strRequest);
//Send the request to PayPal and get the response
Stream RequestStream = req.GetRequestStream();
StreamWriter streamOut = new StreamWriter(RequestStream, System.Text.Encoding.UTF8);
RequestStream.ReadTimeout = 600000;
RequestStream.WriteTimeout = 600000;
streamOut.Write(strRequest);
streamOut.Close(); // EXCEPTION: "The request was aborted: The request was canceled."
答案 0 :(得分:0)
事实证明你必须使用Encoding.ASCII,因为字符串是URL编码的。