Hi I need to send a data to webpage and Read its response I am trying to code it for my project but I am unable to do so ..Please Help..
Below I attach the source code of the webpage where in a user manually types her/his enrollment number and clicls the submit button and desired response is obtained from the webserver but I want to automate it through my application in C#
<HTML>
<HEAD>
<TITLE>Term End Results - June 2011</TITLE>
<h3>Term End Exam Results - June 2011</h3>
<li> <font size=-1> Status as on January 10 ,2012</font></li>
</HEAD>
<BODY bgcolor=Linen>
<form name="Enofr" method="post" action="TermEndJun11.asp" onSubmit="return fnsubmit(this)">
Enter 9 Digit( Numeric) Enrolment Number: <input type="text" name="eno" maxlength="9" size="9">
<input type=hidden name=myhide value=OK> <input type="submit" value="Submit" >
</form>
<font face="Arial, Helvetica, sans-serif" size="2"><b>In case any student is found
to be booked under unfairmeans, the result of the particular student will be Cancelled.</b></font><br>
<script language=VBScript>
function fnsubmit(frm)
if document.forms(0).eno.value="" then
msgbox "Enrolment should not be blank"
document.forms(0).eno.focus
fnsubmit=false
elseif len(document.forms(0).eno.value)<>9 then
msgbox "Enrolment should be of 9 Character"
document.forms(0).eno.focus
fnsubmit=false
else
fnsubmit=true
end if
end function
</script> <p></p>
</BODY>
<script language=javascript>
document.forms(0).item(0).focus()
</script>
<a href="javascript:history.back()">Back </a> </HTML>
The code which I have written to automate the process is given below(kindly note I have modified this code for the forum)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace websiteIgnouReadAtConsole
{
class Program
{
static void Main(string[] args)
{
string uri = "http://somewebsite/TermEndJun11.asp";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-from-urlencoded";
// encode the data to POST:
string postData = "eno=070089127";
byte[] encodedData = new ASCIIEncoding().GetBytes(postData);
request.ContentLength = encodedData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(encodedData, 0, encodedData.Length);
// send the request and get the response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Do something with the response stream. As an example, we'll
// stream the response to the console via a 256 character buffer
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Char[] buffer = new Char[256];
int count = reader.Read(buffer, 0, 256);
while (count > 0)
{
Console.WriteLine(new String(buffer, 0, count));
count = reader.Read(buffer, 0, 256);
}
} // reader is disposed here
} // response is disposed here
Console.ReadLine();
}
}
}
the above code doesnot give the required webpage