1

我想使用 C# 从操作中发布一个表单。

<form method=post action=controller.php>
....
</form>

我已经制作了一个显示在 apsx 页面上的表格。

我的问题是我们如何通过 C# 使用 POST 方法发送表单。注意:我想从 aspx 页面中使用单独的 C# 文件。

是否可以在按钮事件上以编程方式向 controller.php 发送表单?我们在操作页面上收到表单的值。

4

3 回答 3

1

这是我用来从与网页无关的代码中进行 HTTP 发布的代码。它是否适用于你,我不确定。

    public static string HTTP_Post(string url, string data, DataType type = DataType.XML)
    {
        byte[] arr = System.Text.Encoding.UTF8.GetBytes(data);
        return new StreamReader(HTTP_Post_Response(url, arr, type)).ReadToEnd();
    }
    public static string HTTP_Post(string url, FileInfo file, DataType type = DataType.XML)
    {
        StreamReader fs = new StreamReader(file.OpenRead());
        byte[] arr = System.Text.Encoding.UTF8.GetBytes(fs.ReadToEnd());
        fs.Close();
        return new StreamReader(HTTP_Post_Response(url, arr, type)).ReadToEnd();
    }

    private static Stream HTTP_Post_Response(string url, byte[] data, DataType type)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "POST";
        switch (type)
        {
            case DataType.Text:
                request.ContentType = "text/text"; break;
            case DataType.XML:
                request.ContentType = "text/xml"; break;
        }
        request.ContentLength = data.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(data, 0, data.Length);
        dataStream.Close();
        return request.GetResponse().GetResponseStream();

    }

    public enum DataType
    {
        Text = 0,
        XML,
    }

只需HTTP_Post(url, content)从您的代码中调用。

于 2012-09-24T18:42:57.880 回答
0

在 PHP 中,您将表单发布到不同的页面以处理用户输入。

但是,在 asp.net 表单中将 Posts 返回到自身,并且您在代码隐藏文件中的提交按钮的事件处理程序内部进行表单处理。

假设在 Default.aspx 中您没有指定 action 属性,

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox runat="server" ID="yourTextBox"/>
        <asp:Button ID="yourButton" Text="Submit" runat="server" OnClick="yourButton_Click"/>        
    </div>
    </form>
</body>
</html>

当您查看页面源时,操作将填充为相同的页面名称。

代码背后

public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //IsPostBack false when page loads first time
            if (!IsPostBack)  //same as $_POST['Submit'] in PHP
            {
                //use this if block to initialize your controls values
                yourTextBox.Text = "Please enter value";
            }
        }

        protected void yourButton_Click(object sender, EventArgs e)
        {
            //obtain control values
            //do form prcessing
            string userInput = yourTextBox.Text;

            //write your business logic here, 

            //redirect to next page
            Response.Redirect("action.aspx");
        }
    }

有关如何在页面之间传递数据的更多详细信息,请查看此 msdn 链接 -

http://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.100).aspx

我希望这可以帮助你。

于 2012-09-24T18:30:31.143 回答
0

您可以尝试使用此代码 - 基于Socket class

IPHostEntry ipHost = Dns.GetHostEntry("....your adress");//Adjust your adress
IPAddress  ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

// Create a TCP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);

// Connect the socket to the remote endpoint.
client.Connect(ipEndPoint);

// There is a text file test.txt located in the root directory. 
string fileName = "C:\\YourAspx.aspx";

// Send file fileName to remote device
Console.WriteLine("Sending {0} to the host.", fileName);
client.SendFile(fileName);

// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
于 2012-09-24T18:31:03.703 回答