我想要一份獲得來自REST API的服務內容。我有一個Uri,它返回了我的json內容。就像下面這個例子:獲得在C#從REST API服務內容
{
"data": {
"id": "2",
"type": "people",
"attributes": {
"email": "[email protected]",
"name": "My Name",
"gender": "M",
"cpf": null,
"cnpj": null,
"rg": null,
"person-type": "NATURAL"
}
}
}
這是我的代碼,但我不知道,我不能得到的內容。有人可以幫助我。我只想在後面的代碼中獲取這些內容。
async Task InitializeUserData()
{
var AppToken = Application.Current.Properties["AppToken"];
var AppUid = Application.Current.Properties["AppUid"];
var AppClientHeader = Application.Current.Properties["AppClientHeader"];
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.xxx.com/v1/profile");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Access-Token", AppToken.ToString());
client.DefaultRequestHeaders.TryAddWithoutValidation("Client", AppClientHeader.ToString());
client.DefaultRequestHeaders.TryAddWithoutValidation("uid", AppUid.ToString());
HttpResponseMessage response = client.GetAsync("").Result;
if (response.IsSuccessStatusCode)
{
var contents = await response.Content.ReadAsStringAsync();
}
}
}
什麼是錯誤閱讀? – Kzrystof
您的JSON是無效的。 – Valkyrie
基址應爲'https://api.xxx.com/v1'然後調用*變種響應=等待client.GetAsync(「個人資料/ 2」); * –