這是我用postman發送的json體,它起作用了(API回應是布爾型的,所以在這種情況下我得到的是真。 )
這是我用postman發送的json體。
{ "ClubID"/span>。 "A9FEE6F-FBBB-EB11","ContactID"。 "B6F1-43A48402F","LoginDate"。 "2017-11-19T22:00:00 2"}
這就是我試圖用來發布請求并獲得回復的函式。
var http = (HttpWebRequest)WebRequest.Create(url)。
http.Accept = "application/json"。
http.ContentType = "application/json";
http.Headers.Add("user-agent", "Mozilla/4.0(兼容;MSIE 6.0;"
"Windows NT 5.2; .NET CLR 1.0.3705;)")。)
http.Method = "POST"/span>;
String json = Newtonsoft.Json.JsonConvert.SerializeObject(listOfMem)。
UTF8Encoding encoding = new UTF8Encoding() 。
Byte[] bytes = encoding.GetBytes(json);
Stream newStream = http.GetRequestStream()。
newStream.Write(bytes, 0, bytes.Length)。
newStream.Close()。
var response = await http.GetResponseAsync()。
var stream = response.GetResponseStream()。
var sr = new StreamReader(stream)。
var content = sr.ReadToEnd()。
string res = content.ToString()。
uj5u.com熱心網友回復:
既然我們使用.net core,請放棄使用古老的HttpWebRequest,讓他安息吧...
這個模板應該可以作業
//A class that hold the request。
public class MyHttpRequestData
{
public string ClubID { get; set; }
public string ContactID { get; set; }
public DateTime LoginDate { get; set; }
}
// Request; }
var myContent = new MyHttpRequestData(); // Initialize
using var request = new HttpRequestMessage
{
Content = new StringContent(JsonSerializer.Serialize(myContent), Encoding.UTF8, "application/json") 。
方法 = HttpMethod.Post,
RequestUri = new Uri("Url goes here!")
};
using var httpClient = new HttpClient()。
using var response = await httpClient.SendAsync(request);
//做任何與回應有關的事情。
uj5u.com熱心網友回復:
要發布JSON資料到你的API端點,你也可以嘗試以下代碼片段。
using System.Net.Http;
using System.Net.Http.Json;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/322491.html
標籤:
上一篇:鑄造時檢測到堆疊粉碎


