“请求已中止:请求已取消。”在处理PayPal IPN时

时间:2012-01-16 16:18:43

标签: exception utf-8 paypal-ipn

我有以下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."

1 个答案:

答案 0 :(得分:0)

事实证明你必须使用Encoding.ASCII,因为字符串是URL编码的。