using System;
|
using System.IO;
|
using System.Net;
|
using System.Text;
|
|
public static class HttpHelper
|
{
|
/// <summary>
|
/// 发送 POST 请求(Content-Type: application/json)
|
/// </summary>
|
public static string HttpPost(string url, string jsonBody)
|
{
|
var request = (HttpWebRequest)WebRequest.Create(url);
|
request.Method = "POST";
|
request.ContentType = "application/json;charset=UTF-8";
|
|
byte[] data = Encoding.UTF8.GetBytes(jsonBody);
|
request.ContentLength = data.Length;
|
|
// 写入 Body
|
using (var stream = request.GetRequestStream())
|
{
|
stream.Write(data, 0, data.Length);
|
}
|
|
// 接收响应
|
using (var response = (HttpWebResponse)request.GetResponse())
|
{
|
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
{
|
return reader.ReadToEnd();
|
}
|
}
|
}
|
}
|