用到的命名空间 using System.Net;
方法一:
//url:POST请求地址
//postData:json格式的请求报文,例如:{"key1":"value1","key2":"value2"}
public static string PostUrl(string url, string postData)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.Timeout = 800000;//设置请求超时时间,单位为毫秒
req.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(postData);
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream strea