المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : HTTP Post



C# Programming
07-05-2009, 07:32 PM
Hi, suppose i have an imaginary web address like this with all the query parameter:
http://sokim.seltekom.com/sokim/index.php?do=queryWrite.rewrite&varA=89898&post=1&edat=02-07-2009

I'm using a source code from http://blog.brezovsky.net/
(How to Post data in C# . NET HttpWebRequest)

This is the code :

public static string Post(string url, string data)
{
string vystup = null;
try
{
//Our postvars
byte[] buffer = Encoding.ASCII.GetBytes(data);
//Initialisation, we use localhost, change if appliable
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
//Stream s = WebResp.GetResponseStream();
//StreamReader sr = new StreamReader(s);

//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
vystup = _Answer.ReadToEnd();

//Console.ReadLine();

//Congratulations, you just requested your first POST page, you
//can now start logging into most login forms, with your application
//Or other examples.
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return vystup.Trim() + "\n";

}


This is what i do:
static void Main(string[] args)
{
Console.Write(Post("http://sokim.seltekom.com/sokim/index.php",
"?do=queryWrite.rewrite&varA=89898&post=1&edat=02-07-2009"));
Console.ReadLine();
}

The problem is, the Console always give me the login page source code. I think maybe it's because of wrong url or data pass to the function. But after i try many combination (like remove the '?' sign) it keep give me the login page html source.

For information, the web site has a login page where i must enter user id and password
and i have privilege to access it.

Before i run the above code, i manually sign in to the web site (i think so i can have session registered on the server). After that then i run the above code.

What i'm trying to do is, pull the data from the web page on html format, then with a few string function to get the actual data i need from the html source code.
It has many link inside that it will exhausted to do it manually.
I want make a program to pull the data out and save it to my database.

Can anyone help?