1

C# を使用してアクションからフォームを POST したいと考えています。

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

apsx ページに表示されるフォームを作成しました。

私の質問は、C# を介して POST メソッドでフォームを送信する方法です。注:aspxページから別のC#ファイルを使用したい。

ボタンイベントでcontroller.phpにプログラムでフォームを送信することは可能ですか?? アクションページでフォームの値を受け取ります。

4

3 に答える 3

1

これは、Web ページに関連しないコードから 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フォームではそれ自体にポストバックし、コードビハインドファイルの送信ボタンのイベントハンドラー内でフォーム処理を行います。

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

これがお役に立てば幸いです。

于 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 に答える