How to make a Http request from a .net window form application

  • Thread starter Thread starter Kueishiong Tu
  • Start date Start date
K

Kueishiong Tu

I have a .net window form application but I have to get
data from various web sites. How do I make Http request
(preferrably via post method) from a window form to get
the data from those web sites?
What namespace and dll do I have to include?
A walkthrough and coding example will be helpful.
...
 
I have a .net window form application but I have to get
data from various web sites. How do I make Http request
(preferrably via post method) from a window form to get
the data from those web sites?
What namespace and dll do I have to include?
A walkthrough and coding example will be helpful.


Hi,

This is a sample that I have done recently:

Namespace of the types is:

using namespace System::Net;



System::Net::HttpWebRequest *Request =

__try_cast <System::Net::HttpWebRequest *>

(System::Net::HttpWebRequest::Create (Shttp://www.somesite.com));

String *Content, *Identifier;



// Do the actual request

//

String *Query = "IDENT=";

Query = String::Concat (Query, Ident);

Query = String::Concat (Query, "&USERLOGID=");

Query = String::Concat (Query, UserLogid);

Query = String::Concat (Query,
"&ENTRYLENGTH=-1&EXTRAINPUT=&EXTRAINPUTLENGTH=-1&ENTRY=");

Query = String::Concat (Query, Question);



Request->Method = S"POST";

Request->ContentType = S"application/x-www-form-urlencoded;
charset=\"utf-8\"";

Request->ContentLength = Query->Length;

StreamWriter *sw = new StreamWriter (Request->GetRequestStream ());

sw->Write (Query);

sw->Close ();

WebResponse *Response;

try {

Response = Request->GetResponse ();

} catch (Exception *) {

p->GetIdent ();

return;

}

Content = (new StreamReader (Response->GetResponseStream ()))->ReadToEnd ();

Content contains the response of the webserver.

I hope that helped,
Felix Arends
 
Back
Top