/// <summary>
/// API接口请求
/// </summary>
/// <param name="url"></param>
/// <param name="content"></param>
/// <param name="token"></param>
/// <returns></returns>
public static string Post(string url, string content, string token)
{
string result = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json";
if (token != "")
{
req.Headers.Add("Authorization", token.Replace("\"", ""));
}
byte[] data = Encoding.UTF8.GetBytes(content);
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}
API接口Post请求
最新推荐文章于 2024-09-04 12:00:46 发布