1

我有一个 C# 购物车应用程序,需要将一些数据发布到 PHP 页面并将用户重定向到该页面以查看数据。一切正常!那么,问题出在哪里??

由于我们使用 Javascript 函数通过将其操作设置为 PHP URL 来将表单发布到 PHP 页面,因此不允许我们使用购物车内容清除 Session 变量。

一旦用户单击结帐并被发送到第三方站点,我们希望存储其购物车内容的会话变量消失。据我所知,我无法通过 Javascript 清除这一点,所以我的想法是通过 C# 代码将 POST 数据和用户发送到 PHP 页面。

当用户单击结帐时,Javascript 会重新加载页面,将购物车数据设置为字符串变量,清除会话,然后 POST 数据并将用户发送到 PHP 页面。

除了 POST 数据和重定向用户之外,所有这些都正常工作。不幸的是,出于安全原因,第三方页面不能接受 URL.PHP?=var 类型参数,所以我们必须发布它。

使用 WebRequest 我相信我可以发布数据,但我无法将用户重定向到该页面以完成他们的订单。有任何想法吗?

4

3 回答 3

2

我建议您实现一个中间页面来为您准备数据并清理会话。“结帐”链接将简​​单地将用户导航到此中间页面,该页面将执行以下操作:

  1. 从会话中收集用户的购物车数据
  2. 清除会话
  3. 使用 WebRequest POST 到 PHP 页面

WebRequest上的 MSDN :

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main ()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream ();
            // Write the data to the request stream.
            dataStream.Write (byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}
于 2009-07-22T19:19:55.983 回答
0

您可以继续使用 Javascript 解决方案,只需添加一个将放弃会话的 Ajax 调用

于 2009-07-22T18:08:48.060 回答
0

我只是在这里推测,但您应该能够在 WebBrowser 控件项中传输数据,这样它将发送发布数据并重定向。

于 2010-04-22T10:32:24.730 回答