开发者

Best Matched function for PHP HttpRequest Class in C#

开发者 https://www.devze.com 2023-01-16 18:41 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消