Wednesday, April 27, 2011

Send Web Request from WinForms


When i had to do this long time back i searched a lot and now i am sharing this with all.
Hope this helps somebody
Enjoy Coding :)

StringBuilder sb  = new StringBuilder();


// used on each read operation
byte[]        buf = new byte[8192];


// prepare the web page we will be asking for
HttpWebRequest  request \;
request = (HttpWebRequest)WebRequest.Create("http://www.feefifofum.com/login.aspx?userid=XXX&pass=YYYY");


// execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();


// we will read data via the response stream
Stream resStream = response.GetResponseStream();


string tempString = null;
int    count      = 0;
do
{
    // fill the buffer with data
    count = resStream.Read(buf, 0, buf.Length);


    // make sure we read some data
    if (count != 0)
    {
        // translate from bytes to ASCII text
        tempString = Encoding.ASCII.GetString(buf, 0, count);


        // continue building the string
        sb.Append(tempString);
    }
}
while (count > 0); // any more data to read?


// print out page source
Console.WriteLine(sb.ToString());


Some Others Links:
http://stackoverflow.com/questions/2054712/send-information-through-a-url-from-desktop-based-application/2054819#2054819

http://msdn.microsoft.com/en-us/library/fhd1f0sw(VS.80).aspx

http://msdn.microsoft.com/en-us/library/0645045y(VS.80).aspx

No comments:

Post a Comment