1

I want to POST a Form from action using C#.

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

I have made a form displayed on apsx page.

My question is how we can send the form with POST method through C#. Note : i want to use seperate C# file from aspx pages.

Is it possible to send a form programmatically to controller.php on a button event?? and we receive the values of the form on the action page.

Vedank Kulshrestha
  • 220
  • 1
  • 6
  • 23
  • 3
    This is really a strange question... Makes no sense to me. – Uwe Keim Sep 24 '12 at 18:25
  • Maybe WebClient can do what you want... http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.100).aspx – rene Sep 24 '12 at 18:31
  • @Vedank Do you want to send asp.net form to PHP during postback? – Abhijeet Sep 25 '12 at 02:17
  • @Vedank As many of us are not clear. Please consider updating the question, whether you are looking to postback asp.net webpage to php. Or trying to send control to controller.aspx for form processing ? – Abhijeet Sep 25 '12 at 02:36

3 Answers3

1

This is the code I use to do HTTP posts from code that isn't related to webpages. Whether it's applicable to you or not, I'm not sure.

    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,
    }

Just call HTTP_Post(url, content) from your code.

Bobson
  • 13,498
  • 5
  • 55
  • 80
  • could you explain about the parameters which i should pass?? or you can tell how can i pass the values inside HTTP_POST..? Any Example?? – Vedank Kulshrestha Sep 24 '12 at 19:05
  • `url` is the url that you want to POST to. That'd probably be `/controller.php` or `http://mywebserver.com/controller.php`. `content` is whatever string you want to post - I assume you have something specific you want to past to controller.php, and that's what you'd put there. The third parameter should be `DataType.Text` if you're posting straight text, but it doesn't really matter if you aren't checking it. – Bobson Sep 24 '12 at 19:09
  • could you please explain how you are passing Content through the method?? I want to clearify the way you are passing the content. Hope it works for me . thanks in advance for a help..! – Vedank Kulshrestha Sep 25 '12 at 18:14
  • It depends. If I'm posting a file, I use the version which takes the FileInfo. If I'm posting XML content, I turn that into a string and use the string version. If you're trying to mimic a webform, you'll probably want a bunch of key-value pairs like "SomeData=5&SomeOtherData=10". But it's really based on what content you need to pass. – Bobson Sep 25 '12 at 20:26
0

In PHP you post the form to different page for processing the user input.

However, in asp.net form Posts back to itself, and you do the form processing inside event handler of submit button in the code behind file.

Lets say in Default.aspx you did not specify action attribute,

<%@ 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>

When you view page source, action is populated as same page name.

Code Behind

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");
        }
    }

For more detail on how to pass data among pages, have a look at this msdn link -

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

I hope this helps you.

Abhijeet
  • 13,562
  • 26
  • 94
  • 175
  • What is "SET"? Never heard of such an HTTP verb. – Uwe Keim Sep 24 '12 at 18:32
  • should we get the values of the input textbox inside the form to action page? – Vedank Kulshrestha Sep 24 '12 at 19:04
  • -1 Sorry, this is confusing - `Response.Redirect` doesn't meet any form processing requirement. What does adding `runat="server"` have to do with html form processing? Perhaps the question itself is confusing everyone. – EdSF Sep 25 '12 at 00:47
  • @EdSF Sorry, I could have been more explicit about that. I wanted to highlight OP that - 1.asp.net controls must be placed inside form server control. 2. Unlike PHP, in asp.net we do not postback the page to other page for page processing, rather we do this in same on postaback. – Abhijeet Sep 25 '12 at 02:21
  • @VedankKulshrestha Have updated the code to show you how to do get textBox values. You should do this in same page. – Abhijeet Sep 25 '12 at 02:29
0

You can try with this code - based on 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();
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51