日期:2014-05-17  浏览次数:20416 次

最近在做PayPal接口,一些代码不是很明白,希望和大家讨论下。
        protected void Page_Load(object sender, EventArgs e)
        {
            string strFormValues;
            string strNewValue;
            string strResponse;
            string reqUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUrl);
            //设置request属性
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            byte[] param = HttpContext.Current.Request.BinaryRead(HttpContext.Current.Request.ContentLength);
            strFormValues = Encoding.ASCII.GetString(param);
            //建议在此将接收到的信息记录到日志文件中以确认是否收到IPN信息
            strNewValue = strFormValues + "&cmd=_notify-validate";
            req.ContentLength = strNewValue.Length;
            //发送request
            StreamWriter stOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
            stOut.Write(strNewValue);
            stOut.Close();

            //回复IPN并接受反馈信息
            StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
            strResponse = stIn.ReadToEnd();
            stIn.Close();            

            //确认IPN是否合法
            if (strResponse == "VERIFIED")