public static string HttpPostRequestAsync(string Url, string Api, object obj, string ContentType = “application/json”)
{
string result = “”;
try
{
using (System.Net.Http.HttpClient http = new System.Net.Http.HttpClient())
{
http.DefaultRequestHeaders.Add(“User-Agent”, @“Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)”);
http.DefaultRequestHeaders.Add(“Accept”, @“text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8”);
using (HttpContent content = new JsonContent(obj))
{
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentType);
string url = string.Format("{0}{1}", Url, Api);
var response = http.PostAsync(url, content).Result;
System.IO.Stream myResponseStream = response.Content.ReadAsStreamAsync().Result;
HttpClient json格式访问api
最新推荐文章于 2022-10-25 13:53:13 发布