Reputation: 1
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();
}
}
}
p.s I got this idea from: How do you programmatically fill in a form and 'POST' a web page? But I keep getting this error message: "The request was aborted: The operation has timed out"
Thanks for your anticipated help.
Upvotes: 0
Views: 1605
Reputation: 2561
Ok I think you are misunderstanding what is going on when you request a page from asp.net or any other http server.
First the client requests a page using HTTP. Usually this is the initial GET request for the page like /MyForm.aspx made from a browser
Then the c# code get executed on the server. This creates the html page.
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.
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 method is different then GET because it's intended to have a body section of HTTP. Inside that body portion you have your form data. Usualy the data is formatted as x-www-form-urlencoded string.
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.
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. The error you are getting is related to what ever is running on project-2 since there is no response from the server.
This can be caused but many things (bad proxy setup, long running code on the server, dns issues...)
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.
Upvotes: 1