Best Matched function for PHP HttpRequest Class in C#
I am developing an App w开发者_如何学Pythonhere 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.
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
}
HttpWebRequest http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
精彩评论