如何发布到c#的一种形式 [英] How to Post a form through c#

查看:88
本文介绍了如何发布到c#的一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#职位从操作表单。

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

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

我已经apsx页面上显示的形式。

I have made a form displayed on apsx page.

我的问题是我们如何能够用POST方法,通过C#的形式发送。 注:我想使用独立的C#文件从aspx页面。

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.

是否有可能以编程方式发送一个形式Controller.php这样一个按钮事件? 而我们收到的形式值的操作页面上的。

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.

推荐答案

这是在code我用它来从没有涉及到的网页code执行HTTP岗位。无论是适用于你或没有,我不知道。

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

只需拨打 HTTP_POST(URL,内容)从code。

这篇关于如何发布到c#的一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆