c# 发送post
请求示例:
using System.Net;
using System.IO;
using System.Text;
public string SendPostRequest(string url, string data)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
byte[] byteData = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteData.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(byteData, 0, byteData.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
使用示例:
string url = "http://example.com/api";
string data = "{\"name\":\"John\", \"age\":30}";
string result = SendPostRequest(url, data);
Console.WriteLine(result);
``
原文地址: https://www.cveoy.top/t/topic/hh4k 著作权归作者所有。请勿转载和采集!