我想使用C#从操作中发布一个表单。
<form method=post action=controller.php>
....
</form>
我已经在apsx页面上显示了一个表单。
我的问题是我们如何通过C#发送带有POST方法的表单。 注意:我想从aspx页面使用单独的C#文件。
是否可以在按钮事件上以编程方式将表单发送到controller.php? 我们会在操作页面上收到表单的值。
答案 0 :(得分: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)
即可。
答案 1 :(得分:0)
在PHP中,您将表单发布到不同页面以处理用户输入。
然而,在asp.net表单中Posts返回自身,并在代码隐藏文件中的提交按钮的事件处理程序内进行表单处理。
让我们在Default.aspx中说你没有指定动作属性,
<%@ 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
我希望这会对你有所帮助。
答案 2 :(得分: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();