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>
<form method="post" action="https://requesthanlder.com/abc/weblink.cgi" name="weblink">
Number:
<input type="text" name="routing"/>
Account:
<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#.
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);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
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.