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#


<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>
<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" >

<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"
elseif len(document.forms(0).eno.value)<>9 then
msgbox "Enrolment should be of 9 Character"
end if
end function
</script> <p></p>
 <script language=javascript>

<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

the above code doesnot give the required webpage

Since you KNOW the page being called by "action=", all you need to do is open that page with the parameters.

A method that just returns the response stream would look like this:

protected static Stream GetRepsonseStream(string strURI)
         HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strURI);
         req.Method = "POST";
         //req.ProtocolVersion = HttpVersion.Version10;
         return req.GetResponse().GetResponseStream();

And you could call it like this:

static void Main(string[] args)
         //piecing together the "action" site with its expected parameters
         string uri = "http://website/DaniWeb/TermEndJun11.asp?eno=123123123&myhide=OK";
         using (StreamReader fileWebIn = new StreamReader(GetRepsonseStream(uri)))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.