Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hello Guys,

I need help on posting HTML form, But the form should be posted from C#. The case is, My application is in ASP.NET Using C#. I have the following form:

HTML
<html>
<form method="post" action="https://requesthanlder.com/abc/weblink.cgi" name="weblink">

Number:
HTML
<input type="text" name="routing"/>


Account:
HTML
<input type="text" name="account"  size="20"/>
<input type="hidden" name="op" value="process" />
<input input type="submit" name="submit" value="Submit" />
</form>
</html>

When I hit on Submit Button, It works fine for me (action="https://requesthanlder.com/abc/weblink.cgi" this is for testing only, I have separate URL for this).

The case is I don't want to post from HTML, I want to send this request from my C# code. I tried to implement this but could not succeeded. Below is my code that request for response from C#.
C#
private void test()
{
	string account = "12324567";
	string op="process";
	string routing="3216547"
	String postData = "account=" + account +"&routing="+routing + "&op=" + op;
	WebRequest request = 	WebRequest.Create("https://requesthanlder.com/abc/weblink.cgi");
	request.Method = "POST";
	byte[] byteArray = Encoding.UTF8.GetBytes(postData);
	request.ContentType = "application/x-www-form-urlencoded";
	request.ContentLength = byteArray.Length;
	Stream dataStream = request.GetRequestStream();
	dataStream.Write(byteArray, 0, byteArray.Length);
	// Close the Stream object.
	dataStream.Close();
	// Get the response.
	WebResponse response = request.GetResponse();
	// Display the status.
	Console.WriteLine(((HttpWebResponse)response).StatusDescription);
	// Get the stream containing content returned by the server.
	dataStream = response.GetResponseStream();
	// Open the stream using a StreamReader for easy access.
	StreamReader reader = new StreamReader(dataStream);
	// Read the content.
	string responseFromServer = reader.ReadToEnd();
	// Display the content.

	// Clean up the streams.
	reader.Close();
	dataStream.Close();
response.Close();
}


I think I am doing something wrong, as I am not sending the input data. Can any one help me on this.
Posted
Updated 2-Jul-12 20:22pm
v2
Comments
bbirajdar 3-Jul-12 10:34am    
what are we supposed to understand from this statement - "I tried to implement this but could not succeeded"
Member 14185601 1-Dec-19 6:46am    
Do you find its solution
I am currently facing it when i post from HTML it works fine but when i post from code it returns 400 Badrequest error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900