0

I have a know a website that contains an open database of the result of an academic test. http://nts.org.pk/NTSWeb/PPL_30Sep2012_Result/search.asp

I am expert with C# but newbie to web development.

Usually, using web browser, we can enter and roll number and server sends back the result. E.G. Use my Roll Num: 3912125

What I need to do is, use a C# application to communicate this roll null number and get anything, of my result. (any string is excepted, I will parse out my result from that string.)

How do I send query? when I don't know a list of possible query strings.

I tried this code:

string queryString = "RollNo=3912125";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://nts.org.pk/NTSWeb/PPL_30Sep2012_Result/search.asp");
request.UseDefaultCredentials = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

byte[] requestBytes = Encoding.UTF8.GetBytes(queryString);
request.ContentLength = requestBytes.Length;

using (var requestStream = request.GetRequestStream())
{
    requestStream.Write(requestBytes, 0, requestBytes.Length);
    requestStream.Close();
}
WebResponse response = request.GetResponse();
textBox1.AppendText(((HttpWebResponse)response).StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();

textBox1.AppendText(responseFromServer);

reader.Close();
dataStream.Close();
response.Close();
Erwin
  • 4,757
  • 3
  • 31
  • 41
Umar Hassan
  • 192
  • 3
  • 11

3 Answers3

0

You have to append the querystring to the url like this:

string queryString = "RollNo=3912125";
string url = String.Format(@"http://foo/search.asp?{0}", queryString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
Erwin
  • 4,757
  • 3
  • 31
  • 41
0

You should take a look at the code in my answer to C# https login and download file. It gives a good demonstration of how to perform a POST request. As far as knowing what's valid to use for the query-formatted string in your POST, it's simply a matter of looking for appropriate input elements in the page content. It looks like what you have (RollNo) is correct. You may, however, need to also add the submit button value to your request as well depending on how the server behaves, giving you something like. RollNo=3912125&submit=submit.

Community
  • 1
  • 1
JamieSee
  • 12,696
  • 2
  • 31
  • 47
0

You're most of the way there. Your queryString should look like RollNo=3912125&Submit=+Search+. When you are calling WebRequest.Create, the Url should in fact be http://nts.org.pk/NTSWeb/PPL_30Sep2012_Result/result.asp.

The rest of your code should work, though the answer @JamieSee recommended to you has some very good advice about wrapping things in using blocks correctly

nick_w
  • 14,758
  • 3
  • 51
  • 71