简体   繁体   English

如何使用C#在ASP.NET中以编程方式提交表单

[英]How to programmatically submit a form in ASP.NET using C#

I was asked in my internship to post create a form that submit information to a server, now I am to submit that form programmatically as many times as possible to test the site and try to see if it would break by receiving so much data at once. 在实习期间,我被要求发布一个将信息提交到服务器的表单,现在我要以编程方式尽可能多地提交该表单以测试该站点,并尝试一次接收大量数据来查看它是否会损坏。 Am a newbie and I tried search how to do it but am still not getting it. 我是新手,我尝试搜索该怎么做,但仍然不了解。 This is what I tried. 这就是我尝试过的。

namespace project_1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WebRequest req = WebRequest.Create("http://localhost:68644/project-2");
            string postData = "item1=11111&item2=22222&Item3=33333";

            byte[] send = Encoding.Default.GetBytes(postData);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = send.Length;

            Stream sout = req.GetRequestStream();
            sout.Write(send, 0, send.Length);
            sout.Flush();
            sout.Close();

            WebResponse res = req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream());
            string returnvalue = sr.ReadToEnd();
        }
    }
}

ps I got this idea from: How do you programmatically fill in a form and 'POST' a web page? ps:我的想法来自: 您如何以编程方式填写表格并“发布”网页? But I keep getting this error message: "The request was aborted: The operation has timed out" 但是我一直收到以下错误消息:“请求已中止:操作已超时”

Thanks for your anticipated help. 感谢您的预期帮助。

Ok I think you are misunderstanding what is going on when you request a page from asp.net or any other http server. 好的,我认为您误解了从asp.net或任何其他http服务器请求页面时发生的情况。

  1. First the client requests a page using HTTP. 首先,客户端使用HTTP请求页面。 Usually this is the initial GET request for the page like /MyForm.aspx made from a browser 通常,这是从浏览器对/MyForm.aspx这样的页面进行的初始GET请求

  2. Then the c# code get executed on the server. 然后在服务器上执行c#代码。 This creates the html page. 这将创建html页面。

  3. The client most likely browser gets the page that the server created and renders the html, JavaScript, css... This is done on the client machine this machine can't run c# if you are making a request from the browser. 客户端很可能是浏览器获取服务器创建的页面并呈现html,JavaScript,css ...这是在客户端计算机上完成的,如果您正在从浏览器发出请求,则该计算机无法运行c#。

  4. Then the client fills in the fields in the form and once the submit button is pressed the browser sends another HTTP request from the client to the server now using the POST method. 然后,客户端填写表单中的字段,一旦按下“提交”按钮,浏览器便立即使用POST方法将另一个HTTP请求从客户端发送到服务器。 POST method is different then GET because it's intended to have a body section of HTTP. POST方法不同于GET方法,因为它打算具有HTTP的主体部分。 Inside that body portion you have your form data. 在该正文部分中,您具有表单数据。 Usualy the data is formatted as x-www-form-urlencoded string. 通常,数据的格式为x-www-form-urlencoded字符串。

  5. Then the server runs the server code for post. 然后,服务器运行服务器代码以进行发布。 Best way to see what is going on there is to use Fiddler it's an http proxy intended to show you http requests. 了解Fiddler的最佳方法是使用Fiddler,这是一个HTTP代理,旨在向您显示http请求。

What your code is doing right now is creating the post request of the whole process. 您的代码现在正在做的事情是创建整个过程的发布请求。 And the c# code works fine on this side. C#代码在这一方面工作正常。 The error you are getting is related to what ever is running on project-2 since there is no response from the server. 由于服务器没有响应,因此您收到的错误与project-2上正在运行的错误有关。

This can be caused but many things (bad proxy setup, long running code on the server, dns issues...) 这可能是由很多原因引起的(错误的代理设置,服务器上的长时间运行的代码,dns问题...)

BUT most well written servers will prevent you from doing this by design. 但是写得最好的服务器将阻止您设计使然。 It's a security issue and should not be simple to do this. 这是一个安全问题,并且执行起来并不简单。 So you might be getting no response by design. 因此,您可能不会因设计而得到任何回应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发送带有数据的HTTP请求并使用C#提交asp.net表单 - Send HTTP request with data and submit asp.net form using C# 使用c#/ ASP.NET以编程方式伪造登录到网站 - Using c#/ASP.NET to programmatically fake a login to a website 如何使用提交按钮发布 SQL 数据 asp.net c# razor - How do I post SQL data using submit button in asp.net c# razor 如何在ASP.NET中以C#作为代码,以编程方式设置(使用GET SET属性)“ httpRuntime maxRequestLength” - How to programmatically set (using GET SET property) “httpRuntime maxRequestLength” in ASP.NET with C# as code behind 如何使用C#以编程方式运行ASP.Net开发服务器? - How can I programmatically run the ASP.Net Development Server using C#? 使用jQuery和ASP.NET提交表单 - Using jQuery with ASP.NET to Submit a Form 如何在不使用表单的情况下以C#在ASP.NET中创建消息表单 - How do I Make a message form in ASP.NET in C# without using a form 使用 MongoDB C# (ASP.NET) 的登录表单 - Login form using MongoDB C# (ASP.NET) 提交表单时获取客户IP。 (ASP.net C#) - Get Client Ip while submit a form. (ASP.net C#) ASP.NET C#表单提交| 我的功能可以更短,更快吗? - ASP.NET C# Form Submit | Can my function be shorter and faster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM