views:

27

answers:

2

I am developing an App where I would need to perform HTTP Request using C#. Had in been PHP, I could use the HttpRequest class. Which class or group of class is best matched for the PHP HttpRequest by which I could make GET and POST Request.

+1  A: 

Use the HttpWebRequest and HttpWebResponse classes in the System.Net namespace.

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(host));
req.UserAgent = "Test";
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

byte[] data = Encoding.UTF8.GetBytes(postData);

req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
    reqStream.Write(data, 0, data.Length);
}

using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
  //do something with the response
}
Femaref
As I look into the documentation, just verifying how to do Raw POST Data.
Gunner
Just wondering, why is the UserAgent needed?
Gunner
It's not needed, I just included it to show how it's done. You can't set the UserAgent header directly, and in any case, some servers might deny your request if you don't include a UserAgent. It's just an example to show you the ropes, as I remember myself having problems with it in the beginning as well (the whole RequestStream thingy etc).
Femaref